knightnemo's picture
Upload code_segments/segment_218.txt with huggingface_hub
837b66f verified
This is an interactive problem.
Misuki has chosen a secret tree with $n$ nodes, indexed from $1$ to $n$, and asked you to guess it by using queries of the following type:
* "? a b" — Misuki will tell you which node $x$ minimizes $|d(a,x) - d(b,x)|$, where $d(x,y)$ is the distance between nodes $x$ and $y$. If more than one such node exists, Misuki will tell you the one which minimizes $d(a,x)$.
Find out the structure of Misuki's secret tree using at most $15n$ queries!
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 200$) — the number of test cases.
Each test case consists of a single line with an integer $n$ ($2 \le n \le 1000$), the number of nodes in the tree.
It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.
A tree is an undirected acyclic connected graph. A tree with $n$ nodes will always have $n-1$ edges.
In the example case, the answer to "? 1 2" is $1$. This means that there is an edge between nodes $1$ and $2$.
The answer to "? 1 3" is $1$. This means that there is an edge between nodes $1$ and $3$.
The answer to "? 1 4" is $3$. It can be proven that this can only happen if node $3$ is connected to both node $1$ and $4$.
The edges of the tree are hence $(1,2)$, $(1,3)$ and $(3,4)$.