题目描述
Problem Description
The skew heap is a commonly used data structure. It is also a binary tree and satisfies the same heap property as the binary heap: the value of each non-root node is greater than that of its parent. Therefore, in the entire skew heap, the root has the smallest value. However, the skew heap does not need to be balanced, and there are no specific rules regarding the size relationship between the left and right children of each node. In this problem, all elements in the skew heap are unique.
The process of inserting a new element X into a skew heap H is performed recursively: when H is empty or X is less than the root node of H, X becomes the new root, and the original root (if any) becomes the left child of X. When X is greater than the root node of H, the two subtrees of the root node of H are swapped, and X is recursively inserted into the left subtree after the swap.
Given a skew heap containing nodes with values from 0 to n, find a sequence of nodes such that the skew heap can be obtained by inserting these nodes sequentially into an empty tree. If the answer is not unique, output the lexicographically smallest solution. The input guarantees that a solution exists.
Input Format
The first line contains an integer n. The second line contains n integers d1, d2, ..., dn, where di < 100 indicates that i is the left child of di, and di ≥ 100 indicates that i is the right child of di - 100. Clearly, 0 is always the root, so d0 is not included in the input.
Output Format
Output a single line containing n+1 integers, which is the lexicographically smallest insertion sequence.
Sample Input and Output
Sample Input #1
6
100 0 101 102 1 2
Sample Output #1
0 1 2 3 4 5 6
Sample Input #2
6
100 0 2 102 4 104
Sample Output #2
4 6 5 2 0 1 3
Sample Input #3
7
0 100 1 102 2 3 5
Sample Output #3
2 5 0 3 4 6 7 1
Notes
2 ≤ n ≤ 50
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分