#P7597. Interactive Tree Reconstruction
Interactive Tree Reconstruction
Interactive Tree Reconstruction
You are given an interactive problem. There is a rooted tree with n nodes where node \(1\) is the root. Your task is to reconstruct the structure of the tree by asking a limited number of queries. However, for the purpose of offline simulation in our tests, the tree structure is provided as input.
You are allowed to ask two types of queries:
? 1 u v
: This query returns the distance between node \(u\) and node \(v\).? 2 u
: This query returns the size of the subtree rooted at node \(u\) as well as a list of all nodes in that subtree (in arbitrary order).
Once you have determined the tree's structure, output your answer in the following format:
! fa[2] fa[3] ... fa[n]
where \(fa[i]\) is the parent of node \(i\) in the tree. In interactive environments, you must ensure that the total output of your interactions does not exceed 40000 numbers. In our simulation, however, the input is provided as follows.
Note: For simulation purposes, the first input is an integer \(n\) (\(1 \le n \le 10^5\)), followed by \(n-1\) integers representing the parent of each node from \(2\) to \(n\). Your solution should simply output the reconstructed parent's list with the required formatting.
inputFormat
The first line of input contains an integer \(n\), denoting the number of nodes in the tree. In our non-interactive simulation, the second line contains \(n-1\) space-separated integers. The \(i\)-th integer (for \(2 \le i \le n\)) represents the parent of node \(i\) in the tree.
outputFormat
Output a single line in the following format:
! fa[2] fa[3] ... fa[n]
Here, each fa[i] is the parent of node \(i\) in the tree.
sample
3
1 1
! 1 1
</p>