Hoof and Brain
Time limit4sMemory limit1024 MB
Given a directed graph and two starting tokens, decide for each query whether the brain or the hoof wins the token-moving game.
Problem
Given a directed graph with vertices and edges (, ), Farmer John's cows like to play the following game with two players.
Place two tokens on distinct nodes in the graph. Each turn, one player, the brain, chooses a token that must be moved along an outgoing edge. The other player, the hoof, chooses which edge the token moves along. The two tokens can never be on the same node. If at some point the hoof cannot make a valid move, the brain wins. If the game continues indefinitely, the hoof wins.
You are given queries () giving the starting nodes of the two tokens. For each query, output which player wins.
Input
The first line contains and .
The next lines each contain two integers and , denoting an edge from to .
The graph has no self-loops or multiple edges.
The next line contains .
The final lines each contain two integers and satisfying and , giving the starting nodes of the tokens.
Output
Output a string of length . Each character is B if the brain wins and H if the hoof wins.
Hint
The brain can win the first game by selecting node 5. Then the hoof has no valid move.
The brain can win the last game by selecting node 4 and then node 7. Then the hoof has no valid move.
The hoof wins the other games.