#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 vertices, with vertex as the root. The parent of vertex () is vertex . Each vertex has a value of either or written on it, and initially, vertex () has the value written on it.
You need to handle queries. The -th query () is as follows:
- If the value written on vertex is , change it to ; if it is , change it to . After that, output the answer to the following problem:
- Find the minimum number of operations required to make all vertices have the value by repeatedly performing the following operation:
- Select a vertex. For every vertex on the path from vertex to the selected vertex (inclusive), change the value to if it is , and to if it is .
- Find the minimum number of operations required to make all vertices have the value by repeatedly performing the following operation:
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} $$- All input values are integers.
输出格式
Output lines. On the -th line, output the answer to the -th query.
4
1 1 3
0 1 1 0
3
2
1
4
2
1
1