#P16562. [ICPC 2026 APC] Christmas Tree Un-decoration
[ICPC 2026 APC] Christmas Tree Un-decoration
题目描述

Figure D.1: Christmas tree.
Last Christmas, you had a lovely Christmas tree with vertices, numbered from to and rooted at vertex . For each ( ), vertex is the parent of vertex . The tree is decorated beautifully with ornaments on vertex ( ).
However, it has already been a few months since Christmas, and it is time to take down all the ornaments and put your tree away until next year. Since this process is tedious, you have decided to make it more fun by using only the following operation, which you can perform zero or more times:
Choose a vertex . For each vertex on the unique simple path from vertex to vertex (inclusive), remove exactly one ornament from if there is any left.While you are still determining the minimum number of operations needed, your little child modifies the number of ornaments on the tree. More precisely, your child makes changes. In the -th change, the number of ornaments on vertex is modified to ( ). Note that these changes are persistent; the effect of each change carries over to subsequent changes.
Note that you do not actually perform any operations on the tree.
For the initial configuration and after each change, your task is to determine the minimum number of operations needed to remove all the ornaments from the tree at each moment.
输入格式
The first line of input contains one integer ( ) representing the number of test cases. After that, test cases follow. Each of them is presented as follows.
The first line of each test case contains two integers and ( ; ). The second line contains integers ( for all ). The third line contains integers ( for all ).
The -th of the next lines contains two integers and ( ; ).
The sum of across all test cases in one input file does not exceed .
The sum of across all test cases in one input file does not exceed .
输出格式
For each test case, output lines. The first line should contain the minimum number of operations required for the initial tree. The -th of the following lines should contain the minimum number of operations required after the -th change happens.
2
6 3
1 1 2 3 3
5 3 2 5 2 4
4 1
3 10
1 6
8 6
1 2 3 4 5 5 3
1 1 1 1 1 1 1 1
6 3
8 3
5 5
6 1
3 7
5 1
11
9
13
13
3
5
7
8
8
8
7
提示
Explanation for the sample input/output #1
For the first test case, its initial tree is illustrated in Figure D.1. Note that the star at vertex in the figure is also an ornament.
- In the initial tree, you can remove all ornaments with operations by choosing vertex five times, vertex twice, and vertex four times.
- After the first change, there is only one ornament on vertex . You need operations; choose vertex once, vertex twice, vertex twice, and vertex four times.
- After the second change, there are ten ornaments on vertex . You need operations.
- After the third change, there are six ornaments on vertex . However, the required number of operations is unchanged.