QOJ.ac

QOJ

時間限制: 2.0 s 記憶體限制: 512 MB 總分: 100

#18023. Juke Artem

统计

给你一棵拥有 $n$ 个节点的树,节点编号为 $0$ 到 $n - 1$。每个节点上都有一个数字。这些数字介于 $0$ 到 $n - 1$ 之间且互不相同(即它们构成一个排列)。如果数字 $x$ 位于第 $x$ 个节点上,则称该数字“在它原本的位置上”。

你被允许交换树上某条边两个端点处的数字。如果在交换之前,这两个数字中至少有一个在它原本的位置上,则此次交换的花费为 $0$;否则花费为 $1$。

要将所有数字都放回它们原本的位置,需要支付的最小总花费是多少?

输入格式

第一行包含一个整数 $n$($2 \le n \le 10^5$),表示树的节点数。

第二行包含 $n - 1$ 个整数 $p_1, p_2, \dots, p_{n-1}$($0 \le p_i < i$)。其中 $p_i$ 描述了一条连接节点 $p_i$ 和 $i$ 的边。

第三行包含 $n$ 个整数 $a_0, a_1, \dots, a_{n-1}$($0 \le a_i < n$)。其中 $a_i$ 表示最初位于第 $i$ 个节点上的数字。保证 $a$ 是一个排列。

输出格式

输出一个整数,表示需要支付的最小总花费。

样例

输入样例 1

2
0
0 1

输出样例 1

0

输入样例 2

2
0
1 0

输出样例 2

1

输入样例 3

3
0 0
0 2 1

输出样例 3

2

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.