luogu#P16361. [BalticOI 2026] Hamilton
[BalticOI 2026] Hamilton
Problem Description
Consider a directed graph with nodes numbered . The graph is called a tournament if there is exactly one edge between all pairs of nodes in either direction. That is, for any pair of distinct nodes and , there is either an edge from to or from to .
A Hamiltonian cycle is a sequence that visits all nodes and returns back to the start, following edges in the graph. For all , there must be an edge from to . Additionally, there must be an edge from to .
You can freely construct a tournament graph of nodes. The numbering of the nodes is then shuffled. By making queries on the edge directions in the shuffled graph, can you find a Hamiltonian cycle?
Interaction
This is an interactive problem. Start by reading two integers and : the number of nodes and the number of test cases.
Next, print lines to describe the tournament graph. On the th of these lines, print characters "0" or "1". A character "1" at position indicates that there is an edge from to . There should be no edge from to itself.
Then, test cases follow. Each test case uses the same graph you provided, but the numbering is shuffled and kept secret by the grader. You may make some number of queries, after which you should report a Hamiltonian cycle.
To make a query, print "? ", where are distinct nodes in the shuffled graph. The grader responds with either ">" if the edge is from to , or "<" if the edge is from to .
Once you have found a Hamiltonian cycle, print "!" followed by integers . Note that the numbers should follow the shuffled numbering. The next test case begins immediately after you have printed the answer.
A testing script can be downloaded here. The beginning of the script contains instructions on how to use it.
5 2
>
>
>
>
>
<
>
>
<
>
01110
00101
00010
01001
10100
? 1 2
? 2 3
? 3 4
? 4 5
? 5 1
! 1 2 3 4 5
? 1 2
? 1 5
? 4 3
? 4 5
? 3 2
! 1 5 4 3 2
Hint
Explanation
The nodes happen to be shuffled to the original order in the first test case and therefore is a Hamiltonian cycle.
In the second case, the node numbers are shuffled to , in this order. The sequence is indeed a Hamiltonian cycle because was one in the original graph.
In the figure below, the original graph is shown on the left and the shuffled graph from the second test case is shown on the right. The two Hamiltonian cycles are highlighted in red.
:::align{center}
:::
Constraints
Scoring
There is only one test input in each subtask, with test cases. In each test case, the numbering of the graph is shuffled uniformly at random. Making more than queries in a single test case results in the verdict WRONG ANSWER.
Let be the average number of queries made by your program among all test cases belonging to a subtask. You receive points for the subtask if is no greater than the specified limit.
| Subtask | Constraints | Points |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
In subtask 4, you receive points according to the following formula:
$$\left\lfloor \frac {25\,000} {\max(750, Q) - 500} - 24 \right\rfloor$$For example, if the average number of queries made by your program is , you get 1 point from the subtask. If , you get 26 points, and if , you get 76 points.