|
<p> |
|
The far-off land of Tamriel is brimming with opportunity! Opportunity for adventure, politics, romance... and, perhaps most importantly of all, commerce. |
|
</p> |
|
|
|
<p> |
|
A group of Khajiit merchants, traditionally known for roaming the countryside selling their wares, have recently set up permanent bazaars in a number of towns. |
|
Having gotten their cat-like paws on a large supply of raw amber and bronze, they're prepared to strategically work together to maximize their profits selling it! |
|
</p> |
|
|
|
<p> |
|
One bazaar has been set up in each of <strong>N</strong>*<strong>M</strong>+1 towns. The towns are numbered from 0 to <strong>N</strong>*<strong>M</strong>, inclusive, |
|
and are connected by roads in a hub-and-spokes arrangement, with town 0 in the center and <strong>N</strong> lines of <strong>M</strong> towns each arranged around it. |
|
The <em>i</em>th such line consists of towns <strong>M</strong>*(<em>i</em>-1)+1 to <strong>M</strong>*<em>i</em>, inclusive, |
|
connected together in order by <strong>M</strong>-1 roads (with one between towns <strong>M</strong>*(<em>i</em>-1)+1 and <strong>M</strong>*(<em>i</em>-1)+2, |
|
another between towns <strong>M</strong>*(<em>i</em>-1)+2 and <strong>M</strong>*(<em>i</em>-1)+3, and so on). |
|
For each line <em>i</em>, there is furthermore a road connecting its first town (<strong>M</strong>*(<em>i</em>-1)+1) to town 0. |
|
Note that each of the <strong>N</strong>*<strong>M</strong> roads may be travelled in either direction, |
|
and that each town may be reached from each other town by following a sequence of roads. |
|
</p> |
|
|
|
<p> |
|
For example, if <strong>N</strong>=4 and <strong>M</strong>=2, the arrangement of towns and roads would look as follows: |
|
</p> |
|
|
|
<img src={{PHOTO_ID:291992558607686}} width="200px" /> |
|
|
|
<p> |
|
Initially, the bazaar in each town <em>i</em> is stocked with either amber (if <strong>X<sub>i</sub></strong> = "A") or bronze (if <strong>X<sub>i</sub></strong> = "B"). |
|
However, in order to satisfy demand, it should end up stocked with a potentially different ware, either amber |
|
(if <strong>Y<sub>i</sub></strong> = "A") or bronze (if <strong>Y<sub>i</sub></strong> = "B"). |
|
It's guaranteed that the number of bazaars initially stocked with amber is equal to the number of bazaars which should end up stocked with amber (consequently, the same holds true for bronze). |
|
</p> |
|
|
|
<p> |
|
In order to accomplish their goal, the Khajiit merchants may repeatedly select a pair of towns which are directly connected by a road, and swap their bazaars' wares. |
|
Please help them determine the minimum number of such swaps required for all <strong>N</strong>*<strong>M</strong>+1 bazaars to end up stocked with the required wares! |
|
This is guaranteed to be possible for every possible valid input. |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of Khajiit groups. |
|
<br />For each group, there is first a line containing the space-separated integers <strong>N</strong> and <strong>M</strong>. |
|
<br />Then follows a line with the length-(<strong>N</strong> * <strong>M</strong> + 1) string <strong>X</strong>, |
|
the characters <strong>X<sub>0</sub></strong> through <strong>X<sub>N*M</sub></strong>. |
|
<br />Then follows a line with the length-(<strong>N</strong> * <strong>M</strong> + 1) string <strong>Y</strong>, |
|
the characters <strong>Y<sub>0</sub></strong> through <strong>Y<sub>N*M</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th group, print a line containing "Case #<em>i</em>: " followed by |
|
one integer, the minimum number of swaps required to stock all of the bazaars with the required wares. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 80 <br /> |
|
1 ≤ <strong>N</strong>, <strong>M</strong> ≤ 1,000,000 <br /> |
|
1 ≤ <strong>N</strong> * <strong>M</strong> ≤ 1,000,000 <br /> |
|
</p> |
|
|
|
<p> |
|
The sum of <strong>N</strong> * <strong>M</strong> across all <strong>T</strong> test cases is no greater than 10,000,000. |
|
</p> |
|
|
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, no swaps are required. |
|
</p> |
|
|
|
<p> |
|
In the second case, bazaars 1 and 2 should swap their goods. |
|
</p> |
|
|
|
<p> |
|
In the third case, the bazaars are initially set up as follows (with ones carrying amber marked in yellow, and ones carrying bronze marked in orange): |
|
</p> |
|
|
|
<img src={{PHOTO_ID:266243124686458}} width="300px"/> |
|
|
|
|
|
<p> |
|
The following sequence of 3 swaps could then be performed to arrive at the required configuration: |
|
</p> |
|
|
|
<img src={{PHOTO_ID:637978470132709}} width="300px"/><br /> |
|
<img src={{PHOTO_ID:950762542025264}} width="300px"/><br /> |
|
<img src={{PHOTO_ID:635336360404128}} width="300px"/><br /> |
|
|