logo AlgoBeat OnlineJudge
登录 注册

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

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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 .

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

输入格式

The input is given in the following format:

  • All input values are integers.

输出格式

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

样例

样例输入 1

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

样例输出 1

2
1
1