I Hate Ready to Program P12 - Wait... Who's Amon?!?

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 512M

Authors:
Problem type

After escaping from ohi's basement, you finally breathe fresh air! However, it's not so easy... you pick up your phone to tell everyone you're okay, but as you fiddle around with the computer's settings, you notice someone extra in the list of administrators... Wait a minute!

Who's this account called "Amon" on the computer? And who does it belong do? You decide to try and sign into the account. The password is "ReadyToProgram"... of course it is. You scour the computer's files and come across a text file named "CALL_HISTORY_DO_NOT_TOUCH". Looking for any evidence that could solve your case, you click the file. Inside is a list of pairs of phone numbers. At first, you're not sure what it means, but after test-calling Colin, who obviously didn't pick up since it's 6pm in the afternoon (Why would he be awake?), your call gets added to the ever-increasing list. It seems ohi had secretly downloaded an app on everyones' phones that lets her track phone calls!

Any one of those numbers in the text file could be Amon. As someone who doesn't pick up calls from unknown numbers, you assume that everyone else is the same, and will only pick up phone calls of phone numbers they recognize. You decide to scroll through the file and find out anyone who has called these phone numbers, to see if you can use these personal connections to find who Amon really is. The file contains L lines, and you have Q questions that you want to ask yourself. Those questions are all the same: "Are the people with phone number A and phone number B connected in any way?" Two people are considered "connected" if there is a chain of phone calls that lead from phone number A to phone number B.

Constraints

1 \leq L,Q \leq 10^6

A_i,B_i,C_i,D_i are 10-digit phone numbers.

Input Specifications

The first line will contain a single integer, L.

The next L lines will contain two integers, A_i and B_i, indicating A_i called B_i.

The next line will contain a single integer, Q, the number of queries.

The next Q lines will contain two integers, C_i and D_i, asking if C_i and D_i are connected.

Output Specifications

For each query, output on a separate line, Y if the two phone numbers are connected, or N otherwise.

Sample Input

5
4163764972 9053985763
6472983645 4163764972
6478823647 4371234567
4371234567 6472983645
9053985763 4371234567
1
4371234567 9053985763

Sample Output

Y

Explanation for Sample Output

4371234567 called 6472983645, who called 4163764972, who called 9053985763. Notice that there are other possible connections to reach 9053985763 from 4371234567.

I Hate Ready to Program Series

Next Problem:
Previous Problem:

Comments

There are no comments at the moment.