Tree Escape
Time limit2sMemory limit512 MB
On a rooted tree where each leaf starts with one piece, players alternately move a piece to its parent and remove it at the root; decide if the first player wins.
- Level
Hard9 of 10
- Topics
- Game theory, Tree, DFS, Dynamic programming
- Solved
- No attempts yet
Problem
Seongwon moves first and Hyeongseok moves second in a board game played on a tree. The board is a tree with vertices numbered from to . Vertex is the root node, and the parent child relation is defined with respect to the root. A node without children is called a leaf node.
The two players alternate moving one piece, with Seongwon to move first. At the start, every leaf node holds exactly one piece. On your turn, choose one piece on the board and move it to the parent of the node it stands on. Several pieces may share one node. If the moved piece reaches the root node, remove it from the board at once. After moving, pass the turn to the opponent. A player who cannot choose a piece because the board holds no pieces loses.
Write a program that looks only at the shape of the board and decides whether Seongwon can win with optimal play.
Input
The first line gives the number of vertices ().
Each of the following lines describes an edge. Each line gives two natural numbers and (, ), meaning vertices and are joined by an edge.
Output
Print Yes if Seongwon can win with optimal play, and print No otherwise.