#P3682. [CERC2016] 自由的套娃 Free Figurines
[CERC2016] 自由的套娃 Free Figurines
Description
Russian nesting dolls are wooden figurines of strictly increasing sizes that can be nested inside one another. Each figurine can be put into a larger figurine, and can itself contain a smaller figurine. At most one figurine can be directly nested inside any figurine, but that inner figurine can itself contain another, and so on.
Given figurines with distinct sizes, they are numbered to in increasing order of size. If figurine is directly nested inside figurine , then we say is the parent of . If a figurine has no parent, we say it is free. A nesting configuration can be represented by specifying the parent of each figurine.
In each step, you may perform exactly one of the following two operations:
- Take a free figurine and directly place it into a larger figurine that currently contains nothing.
- Choose a figurine that is not free (i.e., has a parent) and remove it from its parent.
Given the initial configuration, compute the minimum number of steps to reach the target configuration.
Input Format
The first line contains a positive integer , the number of figurines.
The second line contains integers , giving the parent of each figurine in the initial configuration, where denotes a free figurine.
The third line contains integers , giving the parent of each figurine in the target configuration, where denotes a free figurine.
The input guarantees that both the initial and the target configurations are valid.
Output Format
Output a single integer, the minimum number of operations.
7
3 5 4 0 7 0 0
3 5 0 6 7 0 0
2
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号