A-Skew-ed Reasoning

시간 제한2초메모리 제한2048 MB

요약
주어진 이진 트리가 스큐 힙 삽입으로 만들어질 수 있는지 판정하고, 가능하다면 사전순 최소와 최대 삽입 순열을 구한다.
난이도

어려움10점 중 9점

유형
트리, 그리디, DFS, 구현
정답자
아직 제출이 없습니다

문제

The following is based on a true story – the names have been changed because. . . well, because you always change names in stories like this one.

Professor Taylor Swift is grading a homework assignment on integer skew heaps. A skew heap is a binary tree with an integer stored in each node such that the value in any node is less than or equal to the values in any of its children. Note that the skew heap need not be a perfect binary tree; that is, the left and/or right subtree of any node may be empty.

Inserting a value xx into a skew heap HH is done using the following recursive procedure:

  • If HH is empty, make HH a skew heap consisting of a single node containing xx.

  • Otherwise, let yy be the value in the root of HH.

    • If y<xy < x, swap the two children of the root and recursively insert xx into the new left subtree.
    • If y≥xy ≥ x, create a new node with value xx and make HH the left subtree of this node.

Figure A.1: Example of inserting the value 77 into a skew heap. The nodes storing 44 and 55 (marked in blue) have their children swapped, while the node storing 1111 becomes the left child of the newly inserted node (marked in red).

Now, back to Professor Swift. The homework problem she has assigned asks the students to show the heap that results from inserting a given permutation of the numbers from 11 to nn, in the given order, into an empty heap. Surprisingly, some of the students have wrong answers! That got Professor Swift wondering: For a given heap, is there an input permutation that would have produced this heap? And if so, what are the lexicographically minimal and maximal such input permutations?

입력

The first line of input contains an integer nn (1≤n≤2⋅1051 ≤ n ≤ 2 \cdot 10^5), the number of nodes in the tree. These nodes contain the numbers from 11 to nn exactly. This is followed by nn lines, the iith of which contains two integers ℓ_iℓ\_i and r_ir\_i (i<ℓ_i≤ni < ℓ\_i ≤ n or ℓ_i=0ℓ\_i = 0; i<r_i≤ni < r\_i ≤ n or r_i=0r\_i = 0), describing the values of the left and right children of the node storing ii, where a value of 00 is used to indicate that the corresponding child does not exist. It is guaranteed that this data describes a binary tree.

출력

Output the lexicographically minimal input permutation that produces the given tree under the insertion method for skew heaps, followed by the lexicographically maximal such input permutation. These permutations may coincide, in which case you still need to output both. If no input permutation producing the given tree exists, output impossible.

예제3

  1. 예제 1

    입력
    7
    2 3
    4 5
    6 7
    0 0
    0 0
    0 0
    0 0
    
    예상 출력
    1 3 2 7 5 6 4
    7 1 5 3 2 6 4
    
  2. 예제 2

    입력
    2
    0 2
    0 0
    
    예상 출력
    impossible
    
  3. 예제 3

    입력
    3
    2 0
    3 0
    0 0
    
    예상 출력
    2 3 1
    3 2 1