Kostyanych's Theorem
Contest: Codeforces Round 951 (Div. 2)
Difficulty rating: 2900
Tags: brute force, constructive algorithms, graphs, interactive

This is an interactive problem.

Kostyanych has chosen a complete undirected graph$$$^{\dagger}$$$ with $$$n$$$ vertices, and then removed exactly $$$(n - 2)$$$ edges from it. You can ask queries of the following type:

- "? $$$d$$$" — Kostyanych tells you the number of vertex $$$v$$$ with a degree at least $$$d$$$. Among all possible such vertices, he selects the vertex with the minimum degree, and if there are several such vertices, he selects the one with the minimum number. He also tells you the number of another vertex in the graph, with which $$$v$$$ is not connected by an edge (if none is found, then $$$0$$$ is reported). Among all possible such vertices, he selects the one with the minimum number. Then he removes the vertex $$$v$$$ and all edges coming out of it. If the required vertex $$$v$$$ is not found, then "$$$0\ 0$$$" is reported.

Find a Hamiltonian path$$$^{\ddagger}$$$ in the original graph in at most $$$n$$$ queries. It can be proven that under these constraints, a Hamiltonian path always exists.

$$$^{\dagger}$$$A complete undirected graph is a graph in which there is exactly one undirected edge between any pair of distinct vertices. Thus, a complete undirected graph with $$$n$$$ vertices contains $$$\frac{n(n-1)}{2}$$$ edges.

$$$^{\ddagger}$$$A Hamiltonian path in a graph is a path that passes through each vertex exactly once.

Input format:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of the test cases follows.

The only line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of vertices in the graph.

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

Interaction format:
Interaction for each test case begins with reading the integer $$$n$$$.

Then you can make no more than $$$n$$$ queries.

To make a query, output a line in the format "? $$$d$$$" (without quotes) ($$$0 \le d \le n - 1$$$). After each query, read two integers — the answer to your query.

When you are ready to report the answer, output a line in the format "! $$$v_1\ v_2 \ldots v_n$$$" (without quotes) — the vertices in the order of their occurrence in the Hamiltonian path. Outputting the answer does not count as one of the $$$n$$$ queries. After solving one test case, the program should immediately move on to the next one. After solving all test cases, the program should be terminated immediately.

If your program makes more than $$$n$$$ queries for one test case or makes an incorrect query, then the response to the query will be $$$-1$$$, and after receiving such a response, your program should immediately terminate to receive the verdict Wrong answer. Otherwise, it may receive any other verdict.

After outputting a query, do not forget to output an end of line and flush the output buffer. Otherwise, you will receive 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 documentation for other languages.

The interactor is non-adaptive. The graph does not change during the interaction.

Hacks

To hack, use the following format:

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The only line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of vertices in the graph.

Each of the following $$$(n - 2)$$$ lines should contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — ends of the edge that was removed from the graph. Each edge must not occur more than once.

The sum of $$$n$$$ over all test cases should not exceed $$$10^5$$$.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the original graph looks as follows:

Consider the queries:

- There are no vertices with a degree of at least $$$3$$$ in the graph, so "$$$0\ 0$$$" is reported.
- There are four vertices with a degree of at least $$$2$$$, and all of them have a degree of exactly $$$2$$$: $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$. Vertex $$$1$$$ is reported, because it has the minimum number, and vertex $$$4$$$ is reported, because it is the only one not connected to vertex $$$1$$$. After this, vertex $$$1$$$ is removed from the graph.
- There are three vertices with a degree of at least $$$1$$$, among them vertices $$$2$$$ and $$$3$$$ have a minimum degree of $$$1$$$ (vertex $$$4$$$ has a degree of $$$2$$$). Vertex $$$2$$$ is reported, because it has the minimum number, and vertex $$$3$$$ is reported, because it is the only one not connected to vertex $$$2$$$. After this, vertex $$$2$$$ is removed from the graph.

The path $$$4 - 3 - 1 - 2$$$ is a Hamiltonian path.

In the second test case, the original graph looks as follows:

Consider the queries:

- Vertex $$$1$$$ has a degree of at least $$$3$$$, but it is connected to all vertices, so "$$$1\ 0$$$" is reported. After this, vertex $$$1$$$ is removed from the graph.
- The remaining vertices $$$2$$$, $$$3$$$, and $$$4$$$ have a degree of at least $$$0$$$, but among them vertex $$$4$$$ has the minimum degree of $$$0$$$ (vertices $$$2$$$ and $$$3$$$ have a degree of $$$1$$$). Vertex $$$4$$$ is not connected to both vertices $$$2$$$ and $$$3$$$, so vertex $$$2$$$ is reported (as it has the minimum number). After this, vertex $$$4$$$ is removed from the graph.

The path $$$4 - 1 - 2 - 3$$$ is a Hamiltonian path.

In the third test case, the graph consists of $$$2$$$ vertices connected by an edge.

Example input:
3
4

0 0

1 4

2 3

4

1 0

4 2

2

1 0

Example output:
? 3

? 2

? 1

! 4 3 1 2

? 3

? 0

! 4 1 2 3

? 0

! 2 1

Editorial:
Let's consider the following recursive algorithm. We will store the Hamiltonian path as a double-ended queue, maintaining the start and end.
In case there are only $$$1$$$ or $$$2$$$ vertices left in the graph, the problem is solved trivially.
Suppose we know that the current graph has $$$n$$$ vertices, and there are at least $$$(n - 2)$$$ edges missing. Then the total number of edges in such a graph is at least $$$$$$\frac{n(n - 1)}{2} - (n - 2) = \frac{n^2 - 3n + 4}{2}.$$$$$$
Let all vertices in the graph have a degree of at least $$$(n - 3)$$$, then the total number of edges does not exceed $$$$$$\frac{n(n-3)}{2} = \frac{n^2 - 3n}{2},$$$$$$ which is less than the stated value. Hence, we conclude that there is at least one vertex with a degree greater than $$$(n - 3)$$$.
If there exists a vertex $$$v$$$ with a degree of $$$(n - 2)$$$, then it is sufficient to run our recursive algorithm for the remaining graph. Since $$$v$$$ is only not connected by an edge to one vertex, $$$v$$$ is connected either to the start or the end of the maintained path in the remaining graph. Thus, we can insert the vertex $$$v$$$ either at the beginning or at the end of the path.
Otherwise, let $$$u$$$ be the vertex with a degree of $$$(n - 1)$$$. There is at least one vertex $$$w$$$ with a degree not exceeding $$$(n - 3)$$$. Remove $$$u$$$ and $$$w$$$ from the graph. Notice that the number of edges in such a graph does not exceed $$$$$$\frac{n^2 - 3n + 4}{2} - (n - 1) - (n - 3) + 1 = \frac{n^2 - 7n + 14}{2}$$$$$$ $$$$$$\frac{n^2 - 7n + 14}{2} = \frac{n^2 - 5n + 6}{2} - \frac{2n - 6}{2}$$$$$$ $$$$$$\frac{n^2 - 5n + 6}{2} - \frac{2n - 6}{2} = \frac{(n - 2)(n - 3)}{2} - (n - 3)$$$$$$ $$$$$$\frac{(n - 2)(n - 3)}{2} - (n - 3) = \frac{(n - 2)((n - 2) - 1)}{2} - ((n - 2) - 1).$$$$$$
The invariant is preserved, so we can run the algorithm for the remaining graph. Then, we can arrange the vertices in the following order: $$$w$$$ — $$$u$$$ — $$$s$$$ — ..., where $$$s$$$ — the start of the Hamiltonian path in the remaining graph.
It remains to understand how to use queries.
Make a query $$$d = (n - 2)$$$. Let $$$u$$$ be the second number in the response to our query. If $$$u \neq 0$$$, the degree of vertex $$$v$$$ is $$$(n - 2)$$$. Run our recursive algorithm, and then compare the start and end of the obtained path with $$$u$$$.
Otherwise, if $$$u = 0$$$, it means the degree of vertex $$$v$$$ is $$$(n - 1)$$$. In this case, ask about any vertex with a low degree (this can be done with a query $$$d = 0$$$). Then simply arrange the vertices in the order mentioned above.
We will make no more than $$$n$$$ queries, and the final asymptotic will be $$$O(n)$$$.