This page is still under construction.

Parts of this page are still being built. What you see may change.

Tea Time

Interview

Time limit1sMemory limit128 MB

Summary
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

NN cows (1≤N≤10001 \le N \le 1000), conveniently numbered 1…N1 \ldots N, attend a tea time every day. MM (1≤M≤20001 \le M \le 2000) unique pairs of those cows have already met before the first tea time. Pair ii is given by two different integers AiA_i and BiB_i (1≤Ai≤N1 \le A_i \le N; 1≤Bi≤N1 \le B_i \le N). The input never lists a pair of cows that have met more than once.

At each tea time, any two cows ii and jj that have both met a mutual friend cow kk will meet during that tea time, expanding their circle of acquaintances.

Tea times are held until no new meetings occur. For each of QQ (1≤Q≤1001 \le Q \le 100) queries, determine whether the two cows have met by then. Query jj consists of two different cows XjX_j and YjY_j (1≤Xj≤N1 \le X_j \le N; 1≤Yj≤N1 \le Y_j \le N).

For example, suppose that among cows 11 through 55 we know that cow 22 has met cow 55, cow 22 has met cow 33, and cow 44 has met cow 55; 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 22 meets cow 44 and cow 33 meets cow 55; see (b). In the second tea time, cow 33 meets cow 44; see (c).

Input

  • Line 11: three space-separated integers NN, MM, and QQ.
  • Lines 2…M+12 \ldots M+1: line i+1i+1 contains two space-separated integers AiA_i and BiB_i.
  • Lines M+2…M+Q+1M+2 \ldots M+Q+1: line j+M+1j+M+1 contains query jj as two space-separated integers XjX_j and YjY_j.

Output

  • Lines 1…Q1 \ldots Q: line jj should be Y if the two cows in query jj have met, or N if they have not.

Examples1

  1. Example 1

    Input
    5 3 3
    2 5
    2 3
    4 5
    2 3
    3 5
    1 5
    
    Expected output
    Y
    Y
    N