Codeforces Round 1118 (Div. 2)
C
Far Cities
Codeforces
1600
原题
← B2D →
翻译中…

This is an interactive problem.

翻译中…

Seferoglu lives in a small coastal city called Samsun, a city where not many people study competitive programming. Whenever an event takes place, he has to travel to far cities where he can meet his friends. Since he is tired of calculating the length of his journey exactly each time, he is just curious about the maximum distance he will ever have to travel between two cities in the worst case scenario. Your task is to help him find this distance while keeping the amount of web searches he has to make small.

翻译中…

There is a hidden tree$^{\text{∗}}$ with $n$ vertices. You can make queries. In one query, you choose two vertices $1 \le u, v \le n$ and an integer $0 \le d \le n$; the grader responds with $1$ if $\operatorname{dist}(u, v) \ge d$ and $0$ otherwise. Here, $\operatorname{dist}(u,v)$ denotes the distance$^{\text{†}}$ between vertices $u$ and $v$ in the tree.

翻译中…

Your task is to determine the diameter's length$^{\text{‡}}$ of the tree and any pair of nodes that are this distance apart. You may ask at most $3 \cdot n$ queries.

翻译中…

$^{\text{∗}}$A tree is a connected graph without cycles.$^{\text{†}}$The distance between two nodes in a tree is the number of edges in the unique simple path between these nodes.$^{\text{‡}}$The diameter's length is the largest distance between two vertices.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains $n$ ($2 \le n \le 1000$), denoting the number of vertices in the tree.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.

翻译中…

Interaction

翻译中…

To make a query, first pick $u$, $v$ and $d$ ($1 \le u, v \le n$, $0 \le d \le n$), a pair of nodes and a constant to check the distance between them against, and output the following line (without quotes):

翻译中…
  • "? u v d"
翻译中…

Afterwards, you should read one single integer ($1$ or $0$), whether $d$ was less than or equal to $\text{dist}(u, v)$ or not.

翻译中…

Note you may ask at most $3 \cdot n$ such queries.

翻译中…

Next, if your program has found an answer, it should print the following line (without quotes):

翻译中…
  • "! u v d"
翻译中…

For some candidate $u$ and $v$ nodes denoting the endpoints of a diameter ($1 \le u, v \le n$) and the length of the path between them ($0 \le d \le n-1$). If there are multiple answers, any one of them may be printed.

翻译中…

Note that this action is not counted towards the maximum query limit.

翻译中…

The grader is non-adaptive. This means that the graph is fixed at the beginning and will not change based on your interactions.

翻译中…

After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.

翻译中…

If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

翻译中…

Hacks

翻译中…

To hack, use the following format:

翻译中…

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

翻译中…

The first line of each test case contains $n$ ($2 \le n \le 1000$), denoting the number of vertices in the tree.

翻译中…

The next $n-1$ lines of each test case contain two integers each $u_i, v_i$ ($1 \le u_i, v_i \le n$), denoting the edges of the tree.

翻译中…

$^{\text{∗}}$To flush, use: fflush(stdout) or cout.flush() in C++; sys.stdout.flush() in Python; see the documentation for other languages.

翻译中…

Note

翻译中…

The hidden graph in the first test case is $(1,2), (2,3), (3,4)$.

翻译中…

The hidden graph in the second test case is $(1,2)$.

翻译中…

The hidden graph in the third test case is $(1,2), (1,3), (1,4)$.

翻译中…

In the third test case, the answer "! 3 4 2" is also correct.

翻译中…

imageimageimageThe tree of the first testcaseThe tree of the second testcaseThe tree of the third testcase

翻译中…

样例

Input #1
3

4

1

0

0

0

2

4

1

1

1

1

0

0

0

0
Output #1
? 1 2 1

? 1 2 2

? 2 3 2

? 3 4 2

! 1 4 3

! 1 2 1

? 1 2 1

? 1 3 1

? 1 4 1

? 3 4 2

? 3 4 3

? 1 2 2

? 1 3 2

? 1 4 2

! 4 2 2
InputCopy OutputCopy
题目信息
题号
2258C
时间限制
2 s
内存限制
256 MB
标签
交互题
树
双指针

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