What Happens When All the Branches Fall From a Tree?
Time limit1sMemory limit1024 MB
Two players take turns removing a root-to-vertex path from a tree, and a Nim game on the removed stones decides each turn. The task is to find the winner.
- Level
Hard8 of 10
- Topics
- Tree, Game theory, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
문제
가지런하다 (even and tidy)
Dongwoo and Jaewoo play a game on a tree with vertices, numbered to . The root of the tree is vertex , and vertex has stones. The game starts with Dongwoo, and the two players take turns. A turn is defined as follows.
Let A be the player whose turn it is, and B the other player. Let be the current forest. At the start, .
- A picks one tree in . If no tree remains, A loses.
- Let be the chosen tree, and let be its root.
- A picks a vertex in and takes every vertex on the path from to , including both endpoints. If , A takes only .
- Using the taken vertices, B and A play a Nim game, starting with B. Players alternate. On each move, a player picks a vertex with at least one stone and takes any positive number of stones from it. The player who cannot move loses.
- If B wins this Nim game, B immediately wins the original game.
- If A wins the Nim game, A rearranges as follows. For each vertex directly connected to in , add the subtree rooted at that vertex to , except the subtree that contains . Then remove from .
The forest starts with one tree, but as the game goes on, it can become a collection of several trees that are not connected to each other.
A player who cannot act on their turn loses. Assuming both players play optimally, determine who wins.
Input
The first line contains the number of vertices ().
The second line contains the stone counts (), separated by spaces. Vertex has stones.
Each of the next lines contains two vertex numbers (, ), separated by a space, that are directly connected in the tree.
The input is guaranteed to be a tree.
Output
If Dongwoo wins, print kidw0124 on the first line. If Jaewoo wins, print eoaud0108.
Hint
The title of this problem and the answer to it are the idea of kidw0124.