QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓

#14406. 翻转

统计

给你一棵拥有 $n$ 个节点的有根树,其中节点 1 为根。节点 $i$($2 \le i \le n$)的父节点为 $p_i$。每个节点上都写有一个数字 0 或 1。初始时,节点 $i$($1 \le i \le n$)上写的数字为 $a_i$。

定义对节点 $i$ 进行节点翻转(vertex flip)操作为:如果节点 $i$ 上的数字为 0,则将其变为 1;如果为 1,则将其变为 0。

定义对节点 $i$ 进行路径翻转(path flip)操作为:对从根节点(节点 1)到节点 $i$ 的路径上的每个节点(包括端点)都进行一次节点翻转操作。

你需要处理 $q$ 次询问。第 $i$ 次询问($1 \le i \le q$)如下:

  • 首先,对节点 $x_i$ 进行一次节点翻转。
  • 然后,求出并输出使所有节点上的数字都变为 0 所需的最少路径翻转操作次数。

可以证明,总是可以通过有限次路径翻转操作使所有节点的值都变为 0。

每次询问带来的修改会保留。例如,第二次询问是在对节点 $x_1$ 进行节点翻转、再对节点 $x_2$ 进行节点翻转之后的树上进行的。

输入格式

第一行包含一个整数 $n$($2 \le n \le 2 \cdot 10^5$)。

第二行包含 $n - 1$ 个整数 $p_i$。其中第 $i$ 个整数是节点 $i + 1$ 的父节点($1 \le p_i \le i$)。

第三行包含 $n$ 个整数 $a_i$,表示节点上的初始值($0 \le a_i \le 1$)。

第四行包含一个整数 $q$($1 \le q \le 2 \cdot 10^5$)。

接下来的 $q$ 行,每行包含一个整数 $x_i$,表示第 $i$ 次询问的参数($1 \le x_i \le n$)。

输出格式

输出 $q$ 行。第 $i$ 行输出第 $i$ 次询问的答案。

样例

输入样例 1

4
1 1 3
0 1 1 0
3
2
1
4

输出样例 1

2
1
1

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.