QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 1024 MB 總分: 100 互動 可 Hack ✓

#15314. Menji, we miss you!

统计

This is an interactive problem.

Menji is lost and everyone misses him. It's your job to find him!

There is a binary tree $T$, consisting of $n$ vertices, and each edge of tree $T$ is of length $1$. Menji is hiding at vertex $X$. You know the structure of the tree. However, you don't know $X$.

To find $X$, you can send signals. You can select a vertex $u$ and select a signal strength $k$, then send a signal of strength $k$ from vertex $u$. If the distance between $u$ and $X$ is no more than $k$, Menji will receive the signal and send a signal back, and you will receive the signal. Otherwise, you won't receive anything.

Sending signals is slow, and you are in a hurry, so please determine the position of Menji in no more than $40$ signals.

Interaction Protocol

The input contains multiple testcases. The first line of the input contains an integer $T$ ($1 \le T \le 100$), the number of testcases.

For each testcase, the first line contains an integer $n$ ($2 \le n \le 3 \times 10^4$), the number of vertices in the tree.

The second line contains $n - 1$ integers $fa_2, fa_3, \dots, fa_n$ ($1 \le fa_i < i$), where $fa_i$ is the parent of $i$ on the tree. The tree is rooted at vertex $1$.

It is guaranteed that the tree is a binary tree, that is, there doesn't exist $1 < i < j < k \le n$, such that $fa_i = fa_j = fa_k$.

To send a signal, print a single line in the following format:

  • ? u k: Indicate that you create a signal at vertex $u$ with strength $k$. You need to ensure $1 \le u \le n, 0 \le k \le n$. Then you have to read an integer $o$ ($o \in \{0, 1\}$). If you received the signal, or equivalently, $dis(u, X) \le k$, then $o = 1$, otherwise $o = 0$.

To report the answer, print a single line in the following format:

  • ! u: Indicate that you have found $X = u$. You need to move on to the next testcase after printing this, or terminate if there's no more.

For each testcase, you can send at most $40$ signals. Reporting the answer does not count as sending a signal.

If you send more than $40$ signals, or the signal you sent is malformed, or the answer you reported is incorrect, then the interaction will end and you will receive Wrong answer verdict.

Note that the interactor is adaptive, meaning that the answer may change depending on your queries as long as it remains consistent with the constraints and the answers to the previous queries.

It's guaranteed that the sum of $n$ over all testcases does not exceed $3 \times 10^4$.

After printing each line do not forget to output the end of line and flush the output. You may use fflush(stdout) or cout.flush() to flush the stream for C++, use System.out.flush() for Java and stdout.flush() for Python.

Example

Sample Input 1

2
7
1 1 2 2 3 3

1

0

0

7
1 1 2 2 3 3

0

1

Sample Output 1

? 2 1

? 3 2

? 4 0

! 5

? 2 1

? 3 0

! 3

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.