atcoder#ARC112C. [ARC112C] DFS Game
[ARC112C] DFS Game
Score : points
Problem Statement
Takahashi and Aoki will play a game using a rooted tree with vertices, numbered through . The root of the tree is Vertex . For each , the parent of Vertex is . Initially, there is one coin on each vertex. Additionally, there is a piece on Vertex .
Takahashi and Aoki will alternately do the following operation, with Takahashi going first:
- If there is a coin on the vertex occupied by the piece: get the coin and end his turn.
- If there is no coin on the vertex occupied by the piece: move the piece (or end the game) as follows.- If there are vertices with coins on them among the children of the vertex occupied by the piece, choose one such vertex, move the piece to that vertex, and end his turn.
- Otherwise, if the piece is on the root, end the game; if not, move the piece to the parent of the vertex occupied by the piece and end his turn.
- If there are vertices with coins on them among the children of the vertex occupied by the piece, choose one such vertex, move the piece to that vertex, and end his turn.
- Otherwise, if the piece is on the root, end the game; if not, move the piece to the parent of the vertex occupied by the piece and end his turn.
Both Takahashi and Aoki will play optimally to maximize the number of coins they will get. Find the number of coins Takahashi will get.
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print the number of coins Takahashi will get when the two players play optimally.
10
1 2 3 4 5 6 7 8 9
10
There is no choice for the players, and the game will always go as follows, so Takahashi will get every coin.
- Takahashi gets the coin on Vertex .
- Aoki moves the piece to Vertex .
- Takahashi gets the coin on Vertex .
- Aoki moves the piece to Vertex .
- Takahashi gets the coin on Vertex .
- Aoki moves the piece to Vertex .
- Takahashi moves the piece to Vertex .
- Aoki moves the piece to Vertex .
- Takahashi moves the piece to Vertex .
- Aoki moves the piece to Vertex .
- The game ends.
5
1 2 3 1
2
First, Takahashi gets the coin on Vertex . Then, Aoki can move the piece to Vertex or Vertex . If he moves it to Vertex , Aoki will eventually get just the coin on Vertex . On the other hand, if he moves it to Vertex , Aoki will eventually get the coins on Vertex , , and . Since Aoki will play optimally to maximize the number of coins he will get, he will choose to move the piece to Vertex . Thus, Takahashi will get two coins.
10
1 1 3 1 3 6 7 6 6
5