2133C.md (5124B)
1 Source: https://codeforces.com/problemset/problem/2133/C 2 3 C. The Nether 4 5 time limit per test 6 7 3 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 Having recently discovered The Nether, Steve has built a network of $n$ nether portals, each at a different location in his world. 24 25 Each portal is connected in one direction to some number (possibly zero) of other portals. To avoid getting lost, Steve has carefully built the network of portals so that there is no sequence of jumps through portals that will bring you from a location back to itself; formally, the network forms a directed acyclic graph. 26 27 Steve refuses to tell you which portals are connected to which, but he will allow you to ask queries. In each query, you give Steve a set of locations $S = \{s_1, s_2, \ldots, s_k\}$ and a starting location $x \in S$. Steve will figure out the longest path starting at $x$ that only passes through locations in $S$ and tell you the number of locations in it. (If it is impossible to reach any other location in $S$ from $x$, Steve will reply with $1$.) 28 29 As you are looking to obtain the achievement "Hot Tourist Destinations", you want to find any path that visits as many locations as possible. Steve is feeling particularly generous and will give you $2 \cdot n$ queries to find it. 30 31 Input 32 33 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. 34 35 The only line of each test case contains a single integer $n$ ($2 \le n \le 500$) — the number of locations. 36 37 It is guaranteed that the sum of $n^3$ over all test cases does not exceed $500^3$. 38 39 Interaction 40 41 The interaction for each test case begins with reading the integer $n$. Then you can make up to $2 \cdot n$ queries. 42 43 To make a query, output a line in the following format: 44 45 * $\texttt{?}\,\,x\,\,k\,\,s_1\,s_2\,\ldots\,s_k$ 46 47 The jury will return the answer to the query. 48 49 When you find a path with maximum length, output a single line in the following format: 50 51 * $\texttt{!}\,\,k\,\,v_1\,v_2\,\ldots\,v_k$ 52 53 This denotes starting at portal $v_1$, then jumping to $v_2$, then to $v_3$ and so on, ending at $v_k$. 54 55 Note that outputting the answer does not count towards the limit of $2 \cdot n$ queries. 56 57 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. 58 59 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. 60 61 The interactor is not adaptive. The connections between portals do not change during the interaction. 62 63 Hacks 64 65 To perform a hack, use the following format: 66 67 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. 68 69 The first line of each test case contains a single integer $n$ ($2 \leq n \leq 500$) — the number of locations. 70 71 Then, output $n$ lines. The $i$-th line should be of the form $k_i\,v_1\,v_2\,\ldots\,v_{k_i}$ ($0 \le k_i \le n - 1$, $1 \le v_j \le n$, $v_j \ne i$), denoting that the portal at location $i$ is connected in one direction (leads to) locations $v_1, v_2, \ldots, v_{k_i}$. The given network must form a directed acyclic graph. 72 73 The sum of $n^3$ over all test cases must not exceed $500^3$. 74 75 $^{\text{∗}}$To flush, use: 76 77 * fflush(stdout) or cout.flush() in C++; 78 * sys.stdout.flush() in Python; 79 * see the documentation for other languages. 80 81 Example 82 83 Input 84 85 ``` 86 2 87 5 88 89 3 90 91 3 92 93 2 94 95 1 96 97 2 98 99 1 100 101 1 102 ``` 103 104 Output 105 106 ``` 107 ? 1 4 1 2 3 4 108 109 ? 3 3 4 3 2 110 111 ? 5 2 1 5 112 113 ? 2 2 2 4 114 115 ! 4 5 1 4 2 116 117 ? 1 2 1 2 118 119 ? 2 2 1 2 120 121 ! 1 1 122 ``` 123 124 Note 125 126 In the first test case, the network of portals is as follows: 127 128  129 130 * The longest path starting at location $1$ passing only through $\{1, 2, 3, 4\}$ is the path $1 \rightarrow 4 \rightarrow 2$, which has $3$ distinct locations. 131 * The longest path starting at location $3$ passing only through $\{2, 3, 4\}$ is the path $3 \rightarrow 4 \rightarrow 2$, which has $3$ distinct locations. 132 * The longest path starting at location $5$ passing only through $\{1, 5\}$ is the path $5 \rightarrow 1$, which has $2$ distinct locations. 133 * It is impossible to get to any other location in $\{2, 4\}$ starting from $2$, so Steve answers $1$ for that query. 134 135 Using the information from these queries, it is possible to determine that a longest path is $5 \rightarrow 1 \rightarrow 4 \rightarrow 2$. 136 137 In the second test case, the network of portals is as follows: 138 139  140 141 Neither of the portals is connected to the other, so the longest path is a single location. Note that ! 1 2 would also be a valid answer.