#P17121. [ICPC 2025 Shanghai R] Menji, we miss you!

    ID: 19458 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>倍增二分2025上海交互题Special Judge树的遍历ICPC树的重心

[ICPC 2025 Shanghai R] Menji, we miss you!

Problem Description

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

There is a binary tree TT, consisting of nn vertices, and each edge of tree TT is of length 11. Menji is hiding at vertex XX. You know the structure of the tree. However, you don’t know XX.

To find XX, you can send signals. You can select a vertex uu and select a signal strength kk, then send a signal of strength kk from vertex uu. If the distance between uu and XX is no more than kk, 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 4040 signals.

Interaction Protocol

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

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

The second line contains n1n - 1 integers fa2,fa3,,fanfa_2, fa_3, \cdots, fa_n (1fai<i1 \le fa_i < i), where faifa_i is the parent of ii on the tree. The tree is rooted at vertex 11.

It is guaranteed that the tree is a binary tree, that is, there doesn’t exist 1<i<j<kn1 < i < j < k \le n, such that fai=faj=fakfa_i = fa_j = fa_k.

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

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

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

  • ! u! \ u: Indicate that you have found X=uX = 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 4040 signals. Reporting the answer does not count as sending a signal.

If you send more than 4040 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 nn over all testcases does not exceed 3×1043 \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++C++, use System.out.flush() for Java and stdout.flush() for Python.

2
7
1 1 2 2 3 3

1

0

0

7
1 1 2 2 3 3

0

1



? 2 1

? 3 2

? 4 0

! 5


? 2 1

? 3 0

! 3