D. Genokraken
constructive algorithms; data structures; graphs; greedy; implementation; interactive; trees; two pointers
This is an interactive problem.Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies.The monster's nerve system can be structured as a tree\(^{\dagger}\) of \(n\) nodes (really, everything should stop resembling trees all the time\(\ldots\)), numbered from \(0\) to \(n-1\), with node \(0\) as the root.Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values \(p_1, p_2, \ldots, p_{n-1}\) of the tree, where \(p_i\) (\(0 \le p_i < i\)) is the direct parent node of node \(i\) (\(1 \le i \le n - 1\)).She doesn't know exactly how the nodes are placed, but she knows a few convenient facts: If we remove root node \(0\) and all adjacent edges, this tree will turn into a forest consisting of only paths\(^{\ddagger}\). Each node that was initially adjacent to the node \(0\) will be the end of some path. The nodes are indexed in a way that if \(1 \le x \le y \le n - 1\), then \(p_x \le p_y\). Node \(1\) has exactly two adjacent nodes (including the node \(0\)). The tree in this picture does not satisfy the condition, because if we remove node \(0\), then node \(2\) (which was initially adjacent to the node \(0\)) will not be the end of the path \(4-2-5\).The tree in this picture does not satisfy the condition, because \(p_3 \le p_4\) must hold.The tree in this picture does not satisfy the condition, because node \(1\) has only one adjacent node. Gretel can make queries to the containment cell: ""? a b"" (\(1 \le a, b < n\), \(a \ne b\)) — the cell will check if the simple path between nodes \(a\) and \(b\) contains the node \(0\). However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most \(2n - 6\) times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand?\(^{\dagger}\)A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them.\(^{\ddagger}\)A path is a tree whose vertices can be listed in the order \(v_1, v_2, \ldots, v_k\) such that the edges are \((v_i, v_{i+1})\) (\(1 \le i < k\)).
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(4 \le n \le 10^4\)) — the number of nodes in Genokraken's nerve system.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).
In the first test case, Genokraken's nerve system forms the following tree: The answer to ""? 2 3"" is \(1\). This means that the simple path between nodes \(2\) and \(3\) contains node \(0\). In the second test case, Genokraken's nerve system forms the following tree: The answer to ""? 2 3"" is \(1\). This means that the simple path between nodes \(2\) and \(3\) contains node \(0\). The answer to ""? 2 4"" is \(0\). This means that the simple path between nodes \(2\) and \(4\) doesn't contain node \(0\). In the third test case, Genokraken's nerve system forms the following tree: