1806. Free Figurines

时间限制 1000 ms   内存限制 512 MB

A *matryoshka doll* is a set of wooden figurines of increasing sizes that can be nested one inside the other. They are nested by placing a figurine inside a larger figurine which is, in turn, placed inside a yet larger figurine, etc. We may never place more than one figurine directly inside another figurine, no matter the sizes of the figurines involved. We are currently playing with a set of $n$ figurines denoted by integers $1$ through $n$ in the order of increasing size. If a figurine $a$ is placed directly inside a figurine $b$ we say that $b$ is a parent of $a$, and if a figurine has no parent we call it free. A configuration of the whole set can be described by specifying the current parent of each figurine. We are allowed to perform the following steps while playing: - Place a free figurine inside a larger free figurine that is currently empty. - Open a non-empty free figurine and take out the figurine placed directly inside. Find the minimal number of steps to obtain the given target configuration from the given initial configuration.

输入数据

The first line contains an integer $n (1 ≤ n ≤ 100 000)$ — the number of figurines.

The following line contains a sequence of $n$ integers $p_1, p_2, ..., p_n (0 ≤ p_k ≤ n)$ describing the initial configuration. The $k$-th integer $p_k$ is $0$ if the figurine $k$ is free, or the parent of figurine $k$ otherwise.

The following line contains a sequence of $n$ integers $q_1, q_2, ..., q_n (0 ≤ q_k ≤ n)$ describing the target configuration in the same format.

You may assume that both configurations are valid: a figurine is always smaller than its parent and no two figurines have the same parent.

输出数据

Output a single integer — the minimal number of steps.

样例输入

复制
7
3 5 4 0 7 0 0
3 5 0 6 7 0 0 \n
 · · · · · · \n
 · · · · · · \n

样例输出

复制
2 \n

提交

请先 登录

© 2026 FAQs Contact About