QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 256 MB Points totaux : 100

#11094. 免费小雕像

Statistiques

套娃是一组尺寸递增的木制玩偶,可以一个套一个地嵌套在一起。嵌套的方法是将一个玩偶放入一个更大的玩偶中,而这个更大的玩偶又可以放入一个更大的玩偶中,以此类推。无论玩偶的大小如何,我们都不能将超过一个玩偶直接放入另一个玩偶内部。

我们目前正在使用一组 $n$ 个玩偶,按尺寸从小到大依次编号为 $1$ 到 $n$。如果玩偶 $a$ 被直接放入玩偶 $b$ 中,我们称 $b$ 是 $a$ 的父玩偶;如果一个玩偶没有父玩偶,我们称它为自由的。整个集合的配置可以通过指定每个玩偶当前的父玩偶来描述。

在游戏过程中,我们允许执行以下操作: 将一个自由的玩偶放入一个当前为空的、更大的自由玩偶中。 打开一个非空的自由玩偶,取出直接放入其中的玩偶。

请找出从给定的初始配置达到目标配置所需的最少操作步数。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 100\,000$),表示玩偶的数量。

接下来一行包含一个由 $n$ 个整数 $p_1, p_2, \dots, p_n$ ($0 \le p_k \le n$) 组成的序列,描述初始配置。第 $k$ 个整数 $p_k$ 若为 $0$,则表示玩偶 $k$ 是自由的;否则表示玩偶 $k$ 的父玩偶。

接下来一行包含一个由 $n$ 个整数 $q_1, q_2, \dots, q_n$ ($0 \le q_k \le n$) 组成的序列,以相同格式描述目标配置。

你可以假设两种配置都是有效的:玩偶总是比其父玩偶小,且没有两个玩偶拥有同一个父玩偶。

输出格式

输出一个整数,表示最少操作步数。

样例

输入 1

7
3 5 4 0 7 0 0
3 5 0 6 7 0 0

输出 1

2

输入 2

6
2 5 4 0 0 0
2 6 4 5 0 0

输出 2

3

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.