A.I. War (Large)
Time limit5sMemory limit512 MB
Find the smallest connected set of planets from planet 0 that borders planet 1, breaking ties by the largest border, and report both counts.
- Level
Medium6 of 10
- Topics
- BFS, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
A.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.
- Every planet you conquer makes the A.I. stronger, because it sees you as a threat and builds more ships to defend itself.
- The A.I. defends every planet you are currently threatening.
You combined those two facts into a strategy.
- Conquer planets until you threaten the A.I.'s home planet.
- If several ways finish step 1, take the one that conquers the smallest number of planets.
- If several ways satisfy step 2, take the one that threatens the largest number of planets at the end.
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?
Input
The first line contains the number of test cases . The first line of each test case contains the number of planets and the number of wormholes , 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 pairs of the form ,, separated by spaces. Each pair means that a two way wormhole connects planet and planet .
Limits
- All wormholes are distinct: if , then .
- There is at least one way to reach planet 1 from planet 0 through wormholes.
Output
For each test case, print one line in the form Case #x: c t. Here is the test case number starting from 1, is the number of planets you conquer under the strategy, and is the number of planets you threaten at the end. The A.I.'s home planet counts toward .
Notes
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.