atcoder#ABC255F. [ABC255F] Pre-order and In-order
[ABC255F] Pre-order and In-order
Score : points
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.
6
1 3 5 6 4 2
3 5 1 4 6 2
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.
2
2 1
1 2
-1
No binary tree rooted at Vertex satisfies the conditions, so should be printed.