#P8276. [USACO22OPEN] Hoof and Brain P
[USACO22OPEN] Hoof and Brain P
Problem Description
You are given a directed graph with nodes and edges (, ). Farmer John’s cows like to play the following two-player game.
Place two tokens on different nodes of the graph (you may use some cow-related items instead of tokens). Each turn, one player, Brain, chooses a token that must be moved along one outgoing edge. The other player, Hoof, chooses which outgoing edge to use to move that token. The two tokens are not allowed to be on the same node at any time. If at some point Hoof cannot make a legal move, then Brain wins. If the game can continue forever, then Hoof wins.
You are given queries (), each containing the initial nodes of the two tokens. For each query, output which player wins.
Input Format
The first line contains and .
The next lines each contain two integers and , representing an edge from to .
The graph contains no self-loops or multiple edges.
The next line contains .
The last lines each contain two integers and , satisfying and , representing the initial nodes of the tokens.
Output Format
Output a string of length . The character B means Brain wins, and H means Hoof wins.
Note: the time limit for this problem is 4 seconds, which is twice the usual limit.
9 10
1 2
2 3
3 4
4 7
3 5
1 6
6 8
8 9
9 6
7 2
4
1 5
1 2
1 6
2 4
BHHB
Hint
Constraints
Brain can win the first game by choosing node ; then Hoof will have no legal move.
Brain can win the last game by choosing node and then choosing node ; then Hoof will have no legal move.
Hoof wins the other games.
Test Point Properties
- Test points 2-3 satisfy , .
- Test points 4-9 satisfy .
- Test points 10-21 have no additional constraints.
Translated by ChatGPT 5