#P10451. Interactive Hamiltonian Path Sorting
Interactive Hamiltonian Path Sorting
Interactive Hamiltonian Path Sorting
You are given (N) elements labeled (1, 2, \dots, N). For every two distinct elements (a) and (b), the relation is determined by a boolean outcome: either (a < b) or (b < a). These comparisons are antisymmetric (i.e. if (a<b) then (b<a) is false), but they are not necessarily transitive. In other words, the relation among the (N) elements forms a tournament (a complete directed graph) with (\frac{N(N-1)}{2}) edges. It is guaranteed that no two elements are equal.\n\nA known fact in tournament theory is that every tournament contains at least one Hamiltonian path. Your task is to determine such a path (p_1, p_2, \dots, p_N) (i.e. a permutation of ({1,2,\dots,N})) such that for every adjacent pair (p_i, p_{i+1}) with (1 \le i < N), the relation (p_i < p_{i+1}) holds. \n\nThis is an interactive problem. However, to adapt it to an ordinary judge system, the interaction is replaced by a fixed I/O format. You can ask queries only via a provided function (or its equivalent) named compare
which tells you the outcome of the comparison between any two elements. Specifically, for two indices (a) and (b), compare(a, b)
returns true if (a<b) and false otherwise. \n\nAfter deducing a valid ordering, output the sorted sequence (the Hamiltonian path) as an array (i.e. a single line of (N) space-separated integers). Any valid answer will be accepted.\n\nNote: You are allowed to ask at most 10,000 queries. In the code below make sure you implement the compare
function according to the instructions. Any unauthorized modifications may result in Wrong Answer.
inputFormat
The input begins with an integer (N) ((1 \le N\le 100) is recommended due to query limits). Then follow (N) lines, each containing (N) integers separated by spaces. The (j)-th integer of the (i)-th line is either 0 or 1. It is 1 if the relation compare(i, j)
is true (i.e. element (i) is less than element (j)); otherwise, it is 0. Note that for all (i), the (i)-th integer on the (i)-th line will be 0.
outputFormat
Output a single line containing a permutation of (1,2,\dots,N) (space-separated) such that for every adjacent pair (a) and (b) in the output the relation compare(a, b)
holds (i.e. (a<b)).
sample
3
0 1 1
0 0 1
0 0 0
1 2 3