#P5773. [JSOI2016] 轻重路径
[JSOI2016] 轻重路径
Description
JYY has recently learned an advanced technique for processing tree structures, called “heavy-light decomposition”. This technique divides the edges of a tree into light edges and heavy edges. Connected heavy edges form several vertex-disjoint paths on the tree. With heavy-light decomposition, starting from any node and walking to the root, you will pass through at most different heavy paths.
If you are not familiar with heavy-light decomposition, JYY gives a brief introduction here. For any node in a rooted tree, let denote the number of nodes in the subtree rooted at . Among all children of , choose the child with the largest value, and mark the edge as a heavy edge. All edges between and its other children are marked as light edges.
To simplify the problem, JYY only considers a rooted binary tree with nodes. The nodes are numbered from to . If a node has two children with the same value, then by default the edge between and its left child is the heavy edge.
Now JYY wants to perform an additional node-deletion operations. Each time, JYY will randomly delete a current leaf node of the binary tree, and you need to dynamically maintain the heavy-light decomposition of this tree.
For convenient output, after each operation you only need to output the sum of the indices of all nodes pointed to by heavy edges.
If after deleting a node, there exists a node whose two children have the same value, then we keep the heavy-edge choice of the same as it was before this operation was performed.
Input Format
The first line contains an integer .
The next lines: the -th line contains two integers , representing the index of the left child and the right child of node . means node has no left child, and means node has no right child.
The -th line contains an integer , indicating the number of deletion operations performed by JYY.
The -th line contains space-separated positive integers, representing the indices of the leaves deleted by JYY.
The input guarantees that each deletion operation deletes a leaf.
Output Format
Output lines. Each line contains one integer: the sum of the indices of all nodes pointed to by heavy edges in the heavy-light decomposition. The first line corresponds to the initial decomposition, and the following lines correspond to the decomposition after each deletion operation.
8
2 3
4 5
0 0
6 7
0 8
0 0
0 0
0 0
7
6 7 8 5 4 2 3
20
21
15
7
6
2
3
0
Hint
For of the testdata, .
For of the testdata, .
For all testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号