codeforces#P1990E2. Catch the Mole(Hard Version)

    ID: 34787 远端评测题 4000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>binary searchdata structuresdfs and similarinteractivetrees

Catch the Mole(Hard Version)

Description

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.

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.

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.

2
2
1 2

1

6
1 2
1 3
1 4
4 5
5 6

0

0

1
? 2

! 2






? 2

? 6

? 4

! 4

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.