Tree Game
Time limit1sMemory limit64 MB
On a tree, players alternately move a token to an unchosen neighbor from Manco's start vertex; find all start vertices where Manco wins with optimal play.
- Level
Medium7 of 10
- Topics
- Tree, Game theory, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Maňko and Kubko both love games and have discovered a new one: the tree game. First, Maňko chooses a vertex of a tree. Then, starting with Kubko, the players alternately choose a neighbor of the most recently chosen vertex that has not been chosen before. Play continues until a player cannot move; that player loses and the other one wins. Maňko moves first, but Kubko is a seasoned player who never makes a mistake. Determine every vertex from which Maňko can start the game and be guaranteed to win, no matter how Kubko plays.
Input
The first line contains a single integer (), the number of vertices in the tree; the vertices are numbered through . Each of the next lines contains one integer: the -th such line holds , meaning there is an edge joining vertex and vertex . It is guaranteed that .
Output
Print every vertex from which Maňko can start and win regardless of Kubko's play, one per line, in ascending order.