#P14020. [ICPC 2024 Nanjing R] 二叉树
[ICPC 2024 Nanjing R] 二叉树
Description
Given a binary tree with vertices, your task is to find a special vertex in the tree with at most queries. That is to say, is the largest integer such that .
Each query consists of two different vertices and . The interactor will output an integer () as the answer. Let be the number of edges on the simple path from vertex to vertex .
- If , then vertex is nearer to the special vertex. That is, .
- If , then the distances from and to the special vertex are the same. That is, .
- If , then vertex is nearer to the special vertex. That is, .
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 indicating the number of test cases. For each test case:
The first line contains an integer () indicating the number of vertices in the binary tree.
For the following lines, the -th line contains two integers and (), indicating the left and right child of the -th vertex. If , then the -th vertex has no left child; if , then the -th vertex has no right child.
It is guaranteed that the sum of for all test cases will not exceed .
Interaction
To ask a query, output one line. First output \texttt{?} followed by a space, then print two different integers and () separated by a space. After flushing your output, your program should read a single integer indicating the answer to your query.
If you want to guess the special vertex, output one line. First output followed by a space, then print an integer () 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:
- (if you use ) or (if you use ) in C and C++.
- in Java.
- 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
京公网安备 11011102002149号