Friendship Graph
Time limit2sMemory limit128 MB
Decide up to 200000 reachability queries on a directed graph with 2000 vertices, printing 1 when Y is reachable from X.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Topological sort, Bit manipulation
- Solved
- No attempts yet
Problem
Friendship between two people is usually mutual, but not always.
If person trusts person , the friendship graph has a directed edge . can contain without containing .
Person wants to deliver a secret message to another person , either directly or by passing it along a chain of trusted friendships in . The message gets through exactly when you can start at , follow edges in their direction, and arrive at .
You are given queries, each with its own and . Decide for every query whether the message gets through.
Input
The input has two parts. The first part is the friendship graph and the second part is the queries, separated by a blank line for clarity.
The first line contains two integers and . (, )
Each of the next lines contains two integers and , meaning has the directed edge . Vertices are numbered from to , so .
The next line contains the number of queries . ()
Each of the next lines contains two integers and . () A query with can appear.
Output
Print one line per query. Print 1 if the message from reaches , and 0 if it does not. If , print 1.