Yesterday, Vadim found a binary tree
a with root at 0 consisting of
N vertices on the road. However, his favorite is binary tree
b with root at 0 also consisting of
N vertices. Therefore, he decided to transform tree
a into tree
b using the following operation:
-
An arbitrary vertex v, other than the root, is chosen. Its subtree, including the vertex itself, is reattached to another vertex u, which does not belong to the chosen subtree. The result must be a binary tree with root at 0.
Vadim is confident that it is possible to transform the found binary tree into an isomorphic one to his favorite using no more than N transformations. Help him find the sequence of these transformations.
Recall that a
binary tree is a tree in which each vertex is the ancestor of no more than 2 other vertices, and the root has no ancestor. Two rooted binary trees are called
isomorphic if:
-
Both trees consist of a single vertex;
-
The number of children of the roots of these trees is the same, the subtree of each child of the first is isomorphic to the subtree of some child of the second, and the subtree of each child of the second is isomorphic to the subtree of some child of the first.
Input
The first line contains an integer N — the number of vertices in the found and favorite trees (2 ≤ N ≤ 103).
The second line contains N−1 integers pai — the ancestors of the vertices of the found tree numbered from 1 to N−1 (0 ≤ pai ≤ N − 1).
The third line contains N−1 integers pbi — the ancestors of the vertices of the favorite tree numbered from 1 to N−1 (0 ≤ pbi ≤ N − 1).
It is guaranteed that the given trees are binary.
Output
In the first line, output an integer M — the number of operations used (0 ≤ M ≤ N).
In the following M lines, output pairs of numbers v and u — the root of the chosen subtree and the vertex to which this subtree is attached during the current operation (1 ≤ v ≤ N − 1, 0 ≤ u ≤ N − 1). The vertex u cannot be in the subtree of vertex v. The tree obtained after each operation must be binary.
It is guaranteed that a solution exists. If there are multiple solutions, output any of them.
Samples
input | output |
---|
4
0 0 1
0 1 2
| 1
2 3
|
4
2 0 0
0 3 0
| 2
3 1
1 0
|
Problem Author: Vadim Barinov
Problem Source: Ural Regional Contest Qualifier 2023