Codeforces Round 960 (Div. 2)
E2
Catch the Mole(Hard Version)
Codeforces
2600
原题
← E1F →
翻译中…

This is the hard version of the problem. The only difference is the limit on the number of queries.

翻译中…

This is an interactive problem.

翻译中…

You are given a tree of $n$ nodes with node $1$ as its root node.

翻译中…

There is a hidden mole in one of the nodes. To find its position, you can pick an integer $x$ ($1 \le x \le n$) to make an inquiry to the jury. Next, the jury will return $1$ when the mole is in subtree $x$. Otherwise, the judge will return $0$. If the judge returns $0$ and the mole is not in root node $1$, the mole will move to the parent node of the node it is currently on.

翻译中…

Use at most $160$ operations to find the current node where the mole is located.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

翻译中…

Interaction

翻译中…

The first line of each test case contains one integer $n$ ($2 \le n \le 5000$).

翻译中…

The following $n-1$ lines describe the edges of the tree. Each line contains two space-separated integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$), indicating an edge between nodes $u_i$ and $v_i$.

翻译中…

It is guaranteed that the input data represents a tree.

翻译中…

The interactor in this task is not adaptive. In other words, the node where the mole is located at first is fixed in every test case and does not change during the interaction.

翻译中…

To ask a query, you need to pick a vertex $x$ ($1 \le x \le n$) and print the line of the following form:

翻译中…
  • "? x"
翻译中…

After that, you receive:

翻译中…
  • $0$ if the mole is not in subtree $x$;
  • $1$ if the mole is in subtree $x$.
翻译中…

You can make at most $160$ queries of this form for each test case.

翻译中…

Next, if your program has found the current node where the mole is located, print the line of the following form:

翻译中…
  • "! x"
翻译中…

Note that this line is not considered a query and is not taken into account when counting the number of queries asked.

翻译中…

After this, proceed to the next test case.

翻译中…

If you make more than $160$ queries during an interaction, your program must terminate immediately, and you will receive the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

翻译中…

After printing a query or the answer for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict Idleness Limit Exceeded. To do this, use:

翻译中…
  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.
翻译中…

Hacks

翻译中…

To hack, follow the test format below.

翻译中…

The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

翻译中…

The first line of each test case contains one integer $n$ and $x$ ($2 \le n \le 5000$, $1 \le x \le n$) — the size of the tree and the initial position of the mole.

翻译中…

The following $n-1$ lines describe the edges of the tree. Each line contains two space-separated integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$), indicating an edge between nodes $u_i$ and $v_i$.

翻译中…

The input data must represent a tree.

翻译中…

Note

翻译中…

In the first test case, the mole is in node $2$ initially.

翻译中…

For the query "? 2", the jury returns $1$ because the mole is in subtree $2$. After this query, the mole does not move.

翻译中…

The answer $2$ is the current node where the mole is located, so the answer is considered correct.

翻译中…

In the second test case, the mole is in node $6$ initially.

翻译中…

For the query "? 2", the jury returns $0$ because the mole is not in subtree $2$. After this query, the mole moves from node $6$ to node $5$.

翻译中…

For the query "? 6", the jury returns $0$ because the mole is not in subtree $6$. After this query, the mole moves from node $5$ to node $4$.

翻译中…

For the query "? 4", the jury returns $1$ because the mole is in subtree $4$. After this query, the mole does not move.

翻译中…

The answer $4$ is the current node where the mole is located, so the answer is considered correct.

翻译中…

Please note that the example is only for understanding the statement, and the queries in the example do not guarantee to determine the unique position of the mole.

翻译中…

样例

Input #1
2
2
1 2

1

6
1 2
1 3
1 4
4 5
5 6

0

0

1
Output #1
? 2

! 2

? 2

? 6

? 4

! 4
InputCopy OutputCopy
题目信息
题号
1990E2
时间限制
4 s
内存限制
256 MB
标签
二分
数据结构
DFS 及类似
分治
交互题
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。