#P15734. [JAG 2024 Summer Camp #2] Flip Path on Rooted Tree

[JAG 2024 Summer Camp #2] Flip Path on Rooted Tree

题目描述

You are given a rooted tree with NN vertices, with vertex 11 as the root. The parent of vertex ii (2iN2 \leq i \leq N) is vertex pip_i. Each vertex has a value of either 00 or 11 written on it, and initially, vertex ii (1iN1 \leq i \leq N) has the value aia_i written on it.

You need to handle QQ queries. The ii-th query (1iQ1 \leq i \leq Q) is as follows:

  • If the value written on vertex xix_i is 00, change it to 11; if it is 11, change it to 00. After that, output the answer to the following problem:
    • Find the minimum number of operations required to make all vertices have the value 00 by repeatedly performing the following operation:
      • Select a vertex. For every vertex on the path from vertex 11 to the selected vertex (inclusive), change the value to 11 if it is 00, and to 00 if it is 11.

It can be proved that this can be achieved in a finite number of operations.

输入格式

The input is given in the following format:

$$\begin{aligned} &N \\ &p_2 \ p_3 \ \ldots \ p_N \\ &a_1 \ a_2 \ \ldots \ a_N \\ &Q \\ &x_1 \\ &x_2 \\ &\vdots \\ &x_Q \end{aligned} $$
  • 2N200,0002 \leq N \leq 200,000
  • 1Q200,0001 \leq Q \leq 200,000
  • 1pi<i (2iN)1 \leq p_i < i \ (2 \leq i \leq N)
  • 0ai1 (1iN)0 \leq a_i \leq 1 \ (1 \leq i \leq N)
  • 1xiN (1iQ)1 \leq x_i \leq N \ (1 \leq i \leq Q)
  • All input values are integers.

输出格式

Output QQ lines. On the ii-th line, output the answer to the ii-th query.

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