Treasure Hunt Game
Time limit4sMemory limit1024 MB
For each start vertex, decide whether Alice can force the token to hit treasure vertices infinitely often in a two-player token game on an undirected graph.
- Level
Medium7 of 10
- Topics
- Graph, Game theory, BFS
- Solved
- No attempts yet
Problem
The graph has vertices numbered 1 to and undirected edges. Every vertex is connected to at least one edge. Some vertices belong to Alice, and the rest belong to Bob. Some vertices contain a treasure.
Alice and Bob place a token on a vertex of the graph and play a game. At the start of each turn, if the token is on one of Alice's vertices, Alice must choose one edge adjacent to the current vertex and move the token to its other endpoint. Bob does the same on his vertices. The token cannot stay where it is.
When the token visits a vertex with a treasure, the player gets one treasure. If that vertex was visited before, the player gets another treasure each time the token returns to it.
Alice wants the token to visit treasure vertices infinitely often, and Bob wants to prevent this. Precisely, play the game for turns. If the token is on a treasure vertex in at least of those turns, Alice wins. Otherwise, Bob wins.
Assuming both players always play optimally, determine who wins for each starting vertex.
Input
The first line contains the number of test cases . ()
The first line of each test case contains integers and . (, )
The next line contains a string of length . Each character is A or B. If the -th character is A, vertex belongs to Alice. If it is B, vertex belongs to Bob.
The next line contains a string of length . Each character is T or .. If the -th character is T, vertex has a treasure. If it is ., vertex has no treasure.
The next lines each contain two integers and , separated by a space, the endpoints of the -th edge. (, ) No two edges connect the same pair of vertices.
In all test cases, the sum of and the sum of each do not exceed 2,000,000.
Output
For each test case, print a line "Case #", where is the test case number. On the next line, print a string of length made of A or B. The -th character is A if Alice wins when the game starts with the token on vertex , and B if Bob wins.