DAGame
Time limit1sMemory limit1024 MB
Two players take turns moving tokens along DAG edges, and same-colored token pairs move together once they meet. Output the winner under optimal play.
- Level
Hard8 of 10
- Topics
- Game theory, Dynamic programming, Graph, Topological sort
- Solved
- No attempts yet
Problem
Woo-young and Hyeon-cheol bet on a set of Yeongcheol Burger Don Mari meals. Hyeon-cheol made the following game.
A DAG (directed graph without cycles) has nodes and edges. pieces each sit on one of the nodes. There is no limit on how many pieces can sit on a node. Each piece has a color, written as an integer from to . At most two pieces have any given color. Young and Cheol take turns, starting with Young. On a turn, a player picks one piece and moves it along an outgoing edge of its node to the next node. If two pieces of the same color sit on the same node, they stack and move together from then on. Pieces of different colors never affect each other. Pieces may already be stacked before the game starts. A player who cannot make a move on their turn loses. Given the initial graph and pieces, output who wins if both players play optimally.
Input
The first line contains the number of nodes () and the number of edges ().
Each of the next lines contains two distinct integers and (), meaning there is an edge from node to node . The same edge may appear more than once.
The next line contains the number of pieces . ()
Each of the next lines contains two integers and (), meaning piece has color and sits on node . ()
The given graph is guaranteed to be a DAG.
Output
If Young wins, print Young. If Cheol wins, print Cheol.