Find the smallest connected set of planets from planet 0 that borders planet 1, breaking ties by the largest border, and report both counts.
Medium6BFSShortest pathDynamic programmingNo attempts yetTime limit5sMemory limit512 MBA.I. War is a real time strategy game developed by Arcen Games. This problem takes its idea from that game, but you do not need to have played it.
You are fighting an artificial intelligence in a war that decides the future of the galaxy. To bring the A.I. down you have to threaten its home planet. Some pairs of planets are connected by wormholes, and one planet may be connected to any number of other planets.
At the start you own only your own home planet. Each turn you may conquer one planet that you threaten. You threaten a planet if you do not own it and it is connected by a wormhole to one of the planets you own. A planet you conquer is yours from that moment on. Once you threaten the A.I.'s home planet, you may not conquer any more planets.
During the most important lesson in tactical school you learned two things about the A.I.
You combined those two facts into a strategy.
You are given the planets and the wormholes. Following this strategy, how many planets do you conquer on the way to the A.I.'s home planet, and how many planets do you threaten at the end?
The first line contains the number of test cases T. The first line of each test case contains the number of planets P and the number of wormholes W, separated by a space. Your home planet is planet 0 and the A.I.'s home planet is planet 1.
The second line of each test case contains W pairs of the form xi,yi, separated by spaces. Each pair means that a two way wormhole connects planet xi and planet yi.
For each test case, print one line in the form Case #x: c t. Here x is the test case number starting from 1, c is the number of planets you conquer under the strategy, and t is the number of planets you threaten at the end. The A.I.'s home planet counts toward t.
In the first case of the first example you threaten the A.I.'s home planet already, so you conquer nothing.
In the third case of the first example one conquest is enough to threaten the A.I.'s home planet. You end up threatening two planets, and one planet is left that is connected to nothing.
In the fourth case of the first example you threaten the A.I.'s home planet after conquering planets 4 and 5. At the end you threaten planets 6, 2, 3 and 1, and planet 1 is the A.I.'s home planet.
Arcen Games is the company that made A.I. War, and it has no connection with this problem.