Yet Another Contest 3 P4 - Rumour

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 3.0s
Memory limit: 512M

Author:
Problem types

After hearing some nasty rumours about him, Mike is determined to find the source of the rumour! There are N suspects labelled from 1 to N who have been involved in gossiping, with suspect X being the creator of the rumour. Between these suspects, there are N-1 friendships. The i-th friendship indicates that suspects u_i and v_i are friends with each other. Although Mike does not know the value of X, he is aware of all friendships.

The rumour spreads according to the following procedure:

  • On day 0, suspect X will create the rumour.
  • On day 1, suspect X will tell the rumour to all of their friends.
  • Then, on day i (i > 1), the suspects who heard the rumour for the first time on day i-1 will tell the rumour to all of their friends who have not yet heard the rumour.

Josh knows who started the rumour, but he is too scared to directly tell Mike, lest he feel the wrath of suspect X. However, he will be willing to answer up to 17 queries. In each query, Mike chooses an integer Y (1 \le Y \le N) and accuses suspect Y of starting the rumour. If X = Y, then Josh will tell Mike that he has found the source of the gossip. Otherwise, Josh will tell Mike an integer Z (1 \le Z \le N) such that suspect Z was first aware of the rumour on a strictly earlier day than when suspect Y was first aware of the rumour. If there are multiple possible values of Z, then Josh can tell Mike any of them. Recall that suspect X was first aware of the rumour on day 0.

Can you help Mike determine who started the rumour?

Constraints

1 \le N \le 10^5

It is guaranteed that all N suspects will eventually be aware of the rumour.

Subtask 1 [10%]

For 1 \le i < N, u_i = i and v_i = i+1.

Subtask 2 [10%]

For 1 \le i < N, u_i = i+1 and v_i = \lfloor \frac{i+1}{2} \rfloor.

Subtask 3 [80%]

No additional constraints.

Note that all previous subtasks must be passed for this subtask to be evaluated.

Interaction

This is an interactive problem, where you will play the role of Mike and the interactor will play the role of Josh. Note that the interactor is adaptive.

First, you should read in the value of N on a single line, representing the number of suspects.

Then, on the i-th of the following N-1 lines, you should read in two space-separated integers u_i and v_i, representing the i-th friendship.

Then, you should begin making queries. To make a query, output an integer Y (1 \le Y \le N) on a single line, indicating that you will accuse suspect Y of starting the rumour. You should then read in an integer Z on the following line. If X = Y, then Z will be 0, and you should terminate your program to receive an Accepted verdict. Otherwise, Z indicates that suspect Z was first aware of the rumour on a strictly earlier day than when suspect Y was first aware of the rumour.

If at any point you make an invalid query, or make more than 17 queries, the interactor will respond with -1 on the following line. Upon reading this value, you should terminate your program to receive a Wrong Answer verdict. Otherwise, you may receive an arbitrary verdict.

Please note that you may need to flush stdout after each operation, or interaction may halt. In C++, this can be done with fflush(stdout) or cout << flush (depending on whether you use printf or cout). In Java, this can be done with System.out.flush(). In Python, you can use sys.stdout.flush().

Sample Interaction

>>> denotes your output. Do not print this out.

5
1 2
2 3
2 4
1 5
>>> 5
3
>>> 2
4
>>> 4
0

Explanation

In the image above, a line drawn between two suspects indicates a friendship.

In this example, X = 4. Note that:

  • Suspect 4 was first aware of the rumour on day 0.
  • Suspect 2 was first aware of the rumour on day 1.
  • Suspects 1 and 3 were first aware of the rumour on day 2.
  • Suspect 5 was first aware of the rumour on day 3.

First, Mike guesses that suspect 5 started the rumour. Josh responds by telling Mike that suspect 3 was aware of the rumour before suspect 5.

Then, Mike guesses that suspect 2 started the rumour. Josh responds by telling Mike that suspect 4 was aware of the rumour before suspect 2.

Finally, Mike guesses that suspect 4 started the rumour. Since Mike guessed correctly, Josh responds with 0.

As Mike has successfully guessed the suspect in no more than 17 queries, you would receive an Accepted verdict for this test case.


Comments

There are no comments at the moment.