#P14020. [ICPC 2024 Nanjing R] 二叉树

    ID: 13992 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2024交互题Special JudgeICPC南京

[ICPC 2024 Nanjing R] 二叉树

Description

This is an interactive problem.\textbf{This is an interactive problem.}

Given a binary tree with nn vertices, your task is to find a special vertex ss in the tree with at most p=log2np = \lfloor \log_2 n \rfloor queries. That is to say, pp is the largest integer such that 2pn2^p \le n.

Each query consists of two different vertices uu and vv. The interactor will output an integer tt (0t20 \le t \le 2) as the answer. Let d(a,b)d(a, b) be the number of edges on the simple path from vertex aa to vertex bb.

  • If t=0t = 0, then vertex uu is nearer to the special vertex. That is, d(u,s)<d(v,s)d(u, s) < d(v, s).
  • If t=1t = 1, then the distances from uu and vv to the special vertex are the same. That is, d(u,s)=d(v,s)d(u, s) = d(v, s).
  • If t=2t = 2, then vertex vv is nearer to the special vertex. That is, d(u,s)>d(v,s)d(u, s) > d(v, s).

Note that the interactor is adaptive, meaning that the answer for each test case is not pre-determined. The interactor can determine the special vertex according to your queries, as long as its answer does not conflict with the previous queries and answers.

Input Format

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (2n1052 \le n \le 10^5) indicating the number of vertices in the binary tree.

For the following nn lines, the ii-th line contains two integers xix_i and yiy_i (0xi,yin0 \le x_i, y_i \le n), indicating the left and right child of the ii-th vertex. If xi=0x_i = 0, then the ii-th vertex has no left child; if yi=0y_i = 0, then the ii-th vertex has no right child.

It is guaranteed that the sum of nn for all test cases will not exceed 2×1052 \times 10^5.

Interaction

To ask a query, output one line. First output \texttt{?} followed by a space, then print two different integers uu and vv (1u,vn1 \le u, v \le n) separated by a space. After flushing your output, your program should read a single integer tt indicating the answer to your query.

If you want to guess the special vertex, output one line. First output !\texttt{!} followed by a space, then print an integer ss (1sn1 \le s \le n) indicating the special vertex. After flushing your output, your program should continue processing the next test case, or exit immediately if there are no more test cases. Note that your guess does not count as a query.

To flush your output, you can use:

  • fflush(stdout)\texttt{fflush(stdout)} (if you use printf\texttt{printf}) or cout.flush()\texttt{cout.flush()} (if you use cout\texttt{cout}) in C and C++.
  • System.out.flush()\texttt{System.out.flush()} in Java.
  • stdout.flush()\texttt{stdout.flush()} in Python.
2
5
0 0
1 5
2 4
0 0
0 0

1

0

2
0 2
0 0

2








? 5 1

? 1 4

! 2



? 2 1

! 1