QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 512 MB Total points: 100

#3550. Hoof and Brain

Statistics

Given a directed graph with $N$ vertices and $M$ edges ($2 \leq N \leq 10^5$, $1 \leq M \leq 2 \cdot 10^5$), 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, will choose a token that must be moved along an outgoing edge. The other player, the hoof, will choose which edge to move the token along. The two tokens can never be on the same node. If at some point the hoof can't make a valid move, the brain wins. If the game continues indefinitely, the hoof wins.

You are given $Q$ queries ($1 \leq Q \leq 10^5$) indicating the starting nodes of the two tokens. For each query, output which player will win.

Input Format

The first line contains $N$ and $M$.

The next $M$ lines each contain two integers $a$ and $b$, denoting an edge from $a$ to $b$.

The graph does not contain self-loops or multiple edges.

The next line contains $Q$.

The final $Q$ lines each contain two integers $x$ and $y$ satisfying $1\le x,y\le N$ and $x\neq y$, indicating the starting nodes of the tokens.

Output Format

A string of length $Q$, where each character is B for the brain winning and H for the hoof winning.

Note: the time limit for this problem is 4s, twice the default.

Sample Input

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

Sample Output

BHHB

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.

Scoring

  • Test cases 2-3 satisfy $N\le 100$, $M\le 200$.
  • Test cases 4-9 satisfy $N\le 5000$.
  • Test cases 10-21 satisfy no additional constraints.

Problem credits: Danny Mittal

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.