hackercup / 2014 /round3 /restaurant_chains.html
wjomlex's picture
2014 Problems
718fd53 verified
raw
history blame contribute delete
No virus
2.94 kB
<p>
The kingdom of Hackadia has <strong>N</strong> cities that are connected by <strong>M</strong> bidirectional roads. Cities are enumerated from <strong>0</strong> to <strong>N - 1</strong>. To attract tourists, the government has decided to open up some restaurant chains throughout the kingdom. Each restaurant chain will serve some subset of cities, and will have exactly one restaurant in each city of the chosen subset. Thus, each city will have some restaurant chains, and it is possible to have all possible restaurant chains in some city or not to have any restaurant chains in some city.
</p>
<p>
To ensure that tourists do not face a drastic change in the food scenario when traveling from one city to another, the government has decided that the following condition must be satisfied for each pair of cities (<strong>u</strong>, <strong>v</strong>) :
</p>
<p>
<i>
"the number of restaurant chains open in city <strong>u</strong> but not in city <strong>v</strong>" plus "the number of restaurant chains open in city <strong>v</strong> but not in city <strong>u</strong>" should always be equal to the shortest distance between city <strong>u</strong> and city <strong>v</strong>.
</i>
</p>
<p>
It is guaranteed that every two cities are connected by some sequence of bidirectional roads, and the shortest distance between city <strong>u</strong> and city <strong>v</strong> is the number of roads on the shortest path connecting <strong>u</strong> and <strong>v</strong>.
</p>
</p>
Your job is to help the government determine the minimal number of restaurant chains, such that there exist some assignment of the restaurant chains among the cities that satisfy the above condition. If such assignment is not possible print <strong>-1</strong>.
</p>
<h3>Input</h3>
<p>
The first line of the input will be the number of test cases <strong>T</strong>.
<br />
Each of the test cases start with a line containing the numbers <strong>N</strong> and <strong>M</strong>, representing the number of cities and the number of roads respectively. <br />
The next <strong>M</strong> lines of each testcase contain a pairs of integers, representing the cities connected by a bidirectional road in Hackadia. <br />
Each testcase will be followed by an empty line. <br />
</p>
<h3>Output</h3>
<p>
For each test case <strong>i</strong> numbered from 1 to <strong>T</strong>, output "Case #<strong>i</strong>: ", followed by the minimal number of restaurant chains or <strong>-1</strong>.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T </strong>&le; 20 <br />
1 &le; <strong>N</strong> &le; 10,000 <br />
1 &le; <strong>M</strong> &le; 50,000 <br />
</p>
<h3>Explanation</h3>
<p>
In the first example, we can have two restaurant chains (named X and Y) and have chain X serve cities 1 and 2, and have chain Y serve cities 2 and 3. <br />
In the second example, it is not possible to have such restaurant chain assignment. <br />
</p>