2001C.md (3119B)
1 Source: https://codeforces.com/problemset/problem/2001/C 2 3 C. Guess The Tree 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 This is an interactive problem. 22 23 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: 24 25 * "? 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)$. 26 27 Find out the structure of Misuki's secret tree using at most $15n$ queries! 28 29 Input 30 31 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. 32 33 Each test case consists of a single line with an integer $n$ ($2 \le n \le 1000$), the number of nodes in the tree. 34 35 It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$. 36 37 Interaction 38 39 The interaction begins by reading the integer $n$. 40 41 Then you can make up to $15n$ queries. 42 43 To make a query, output a line in the format "? a b" (without quotes) ($1 \le a,b \le n$). After each query, read an integer — the answer to your query. 44 45 To report the answer, output a line in the format "! $a_1$ $b_1$ $a_2$ $b_2$ ... $a_{n-1}$ $b_{n-1}$" (without quotes), meaning that there is an edge between nodes $a_i$ and $b_i$, for each $1 \le i \le n-1$. You can print the edges in any order. 46 47 After $15n$ queries have been made, the response to any other query will be $-1$. Once you receive such a response, terminate the program to receive the Wrong Answer verdict. 48 49 After printing each line, do not forget to output the end of line and flush the output buffer. Otherwise, you will receive the Idleness limit exceeded verdict. To flush, use: 50 51 * fflush(stdout) or cout.flush() in C++; 52 * System.out.flush() in Java; 53 * flush(output) in Pascal; 54 * stdout.flush() in Python; 55 * see the documentation for other languages. 56 57 Hacks 58 59 For hacks, use the following format: The first line contains an integer $t$ ($1 \le t \le 200$) — the number of test cases. 60 61 The first line of each test contains an integer $n$ — the number of nodes in the hidden tree. 62 63 Then $n-1$ lines follow. The $i$-th of them contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le n$), meaning that there is an edge between $a_i$ and $b_i$ in the hidden tree. 64 65 The sum of $n$ over all test cases must not exceed $1000$. 66 67 Example 68 69 Input 70 71 ``` 72 1 73 4 74 1 75 1 76 3 77 ``` 78 79 Output 80 81 ``` 82 ? 1 2 83 84 ? 1 3 85 86 ? 1 4 87 88 ! 1 2 1 3 3 4 89 ``` 90 91 Note 92 93 A tree is an undirected acyclic connected graph. A tree with $n$ nodes will always have $n-1$ edges. 94 95 In the example case, the answer to "? 1 2" is $1$. This means that there is an edge between nodes $1$ and $2$. 96 97 The answer to "? 1 3" is $1$. This means that there is an edge between nodes $1$ and $3$. 98 99 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$. 100 101 The edges of the tree are hence $(1,2)$, $(1,3)$ and $(3,4)$.