Aggressor
Time limit5sMemory limit128 MB
For every split of N countries into aggressors and peaceful, decide the winner of the tank game under optimal play, then count Mirko's and Slavko's wins.
- Level
Hard8 of 10
- Topics
- Game theory, Combinatorics, Simulation
- Solved
- No attempts yet
Problem
Mirko and Slavko have played Risk a thousand times, so they are inventing a new game called Aggressor that uses the same board, a map. The board has countries numbered 1 to , and it is known which pairs of countries are neighbors. Two countries can be neighbors even when they do not share a physical border.
Before the game starts, the two players put a number of tanks in every country and declare some of the countries aggressors. The remaining countries are peaceful. Mirko and Slavko then alternate, one move each. A player who cannot move loses the game. Mirko moves first.
On his turn a player picks one of two kinds of moves.
-
Attack:
- The player picks an aggressor country holding tanks and a neighboring peaceful country holding tanks.
- The move is allowed only when .
- Every tank in destroys one tank in with a missile.
- After the move holds tanks, or 0 tanks when .
-
Help:
- The player picks two neighboring peaceful countries and , holding and tanks.
- The move is allowed only when .
- If is odd, the player first adds one new tank to .
- Then exactly half of the tanks in move to .
Countries belong to neither player. On his turn a player may pick any two neighboring countries as long as the move is allowed.
The board has countries, so there are ways to split them into aggressors and peaceful countries. Mirko and Slavko play one game for each way. Count how many of those games Mirko wins and how many Slavko wins when both play optimally.
For some splits neither player can force a win. If no country is an aggressor, for example, no tank can ever be destroyed, so the game never ends.
Input
The first line contains the integer (), the number of countries.
The second line contains the number of tanks in each country at the start of the game, from country 1 to country . Each of those numbers is a positive integer smaller than 40000.
The third line contains the integer (), the number of neighboring pairs.
Each of the next lines contains the numbers of two countries that are neighbors. No pair appears more than once in this list.
Output
In the first line, print the total number of games Mirko wins.
In the second line, print the total number of games Slavko wins.
Note
Take two countries that are neighbors, each holding 100 tanks. Of the four splits, Mirko wins two and Slavko wins one.
- Both countries are aggressors. Mirko cannot move, so Slavko wins.
- Country 1 is an aggressor and country 2 is peaceful. Mirko destroys every tank in country 2 in a single move, after which Slavko cannot move, so Mirko wins.
- Country 1 is peaceful and country 2 is an aggressor. Mirko wins the same way.
- Both countries are peaceful. A help move never lowers the number of tanks and no country is an aggressor, so a help move is always available no matter how the players move. This split has no winner.