#abc255f. [abc255_f]Pre-order and In-order
[abc255_f]Pre-order and In-order
Problem Statement
Consider a binary tree with vertices numbered . Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has at most one left child and at most one right child.
Determine whether there exists a binary tree rooted at Vertex satisfying the conditions below, and present such a tree if it exists.
- The depth-first traversal of the tree in pre-order is .
- The depth-first traversal of the tree in in-order is .
Constraints
- is an integer.
- is a permutation of .
- is a permutation of .
Input
Input is given from Standard Input in the following format:
Output
If there is no binary tree rooted at Vertex satisfying the conditions in Problem Statement, print .
Otherwise, print one such tree in lines as follows. For each , the -th line should contain and , the indices of the left and right children of Vertex , respectively. Here, if Vertex has no left (right) child, () should be .
If there are multiple binary trees rooted at Vertex satisfying the conditions, any of them will be accepted.
Sample Input 1
6
1 3 5 6 4 2
3 5 1 4 6 2
Sample Output 1
3 6
0 0
0 5
0 0
0 0
4 2
The binary tree rooted at Vertex shown in the following image satisfies the conditions.
Sample Input 2
2
2 1
1 2
Sample Output 2
-1
No binary tree rooted at Vertex satisfies the conditions, so should be printed.