Tea Time
InterviewTime limit1sMemory limit128 MB
Starting from a graph of known meetings, two cows meet whenever they share a mutual friend, and after all rounds settle, answer queries about whether each pair has met.
- Level
Medium5 of 10
- Topics
- Graph, Union-find, BFS, DFS
- Solved
- No attempts yet
Problem
cows (), conveniently numbered , attend a tea time every day. () unique pairs of those cows have already met before the first tea time. Pair is given by two different integers and (; ). The input never lists a pair of cows that have met more than once.
At each tea time, any two cows and that have both met a mutual friend cow will meet during that tea time, expanding their circle of acquaintances.
Tea times are held until no new meetings occur. For each of () queries, determine whether the two cows have met by then. Query consists of two different cows and (; ).
For example, suppose that among cows through we know that cow has met cow , cow has met cow , and cow has met cow ; see (a) below.
2---3 2---3 2---3
\ |\ | |\ /|
1 \ --> 1 | \ | --> 1 | X |
\ | \| |/ \|
4---5 4---5 4---5
(a) (b) (c)
In the first tea time, cow meets cow and cow meets cow ; see (b). In the second tea time, cow meets cow ; see (c).
Input
- Line : three space-separated integers , , and .
- Lines : line contains two space-separated integers and .
- Lines : line contains query as two space-separated integers and .
Output
- Lines : line should be
Yif the two cows in query have met, orNif they have not.