Kotlin Heroes: Episode 10 |
---|
Finished |
You are given two rooted trees, consisting of $$$n$$$ vertices each. The vertices in the trees are numbered from $$$1$$$ to $$$n$$$, and the root is the vertex $$$1$$$.
You can perform the following operation: choose the tree and the vertex $$$v$$$ (except the root of the tree) in it; connect the child nodes of $$$v$$$ to the parent of $$$v$$$ and remove $$$v$$$ from the tree.
Let's say that two trees are equal if both of the following conditions hold:
Your task is to calculate the minimum number of aforementioned operation in order to make the trees equal.
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 40$$$) — the number of vertices in the trees.
The second line contains $$$n-1$$$ integers $$$a_2, a_3, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ the parent of the $$$i$$$-th vertex in the first tree. Vertex $$$1$$$ is the root.
The third line contains $$$n-1$$$ integers $$$b_2, b_3, \dots, b_n$$$ ($$$1 \le b_i \le n$$$), where $$$b_i$$$ the parent of the $$$i$$$-th vertex in the second tree. Vertex $$$1$$$ is the root.
Print a single integer — the minimum number of aforementioned operation in order to make the trees equal.
51 5 5 11 4 1 2
4
211
0
104 7 10 6 2 9 7 1 11 2 10 3 4 6 6 5 5
10
Name |
---|