#P4107. [HEOI2015] 兔子与樱花
[HEOI2015] 兔子与樱花
Description
A long time ago, a group of rabbits lived in a forest. One day, they suddenly decided to go see sakura.
The sakura tree in their forest is special. It consists of branch fork points, numbered from to . These fork points are connected by branches, and we can regard it as a rooted tree where node is the root.
Each node of the tree has some cherry blossoms; specifically, node has blossoms. Every node of the sakura tree has a maximum load . For each node , the sum of the number of its children and the number of blossoms on node must not exceed , i.e., , where denotes the number of children of . If is a leaf, then .
Now the rabbits think there are too many nodes on the sakura tree and want to remove some nodes. When a node is removed, the blossoms on this node and all its children are attached to the parent of the removed node. If the parent is also removed, they continue to attach upward until the first node that is not removed.
The rabbits want to compute the maximum number of nodes that can be removed without violating the maximum load.
Note that the root cannot be removed, and deleted nodes are not counted toward the load.
Input Format
The first line contains two positive integers, and , denoting the number of nodes and the maximum load.
The second line contains integers , where is the number of blossoms on node .
Then follow lines. In each line, the first number is the number of children of this node, followed by integers giving the indices of its children.
Output Format
Output a single integer: the maximum number of nodes that can be removed.
10 4
0 2 2 2 4 1 0 4 1 1
3 6 2 3
1 9
1 8
1 1
0
0
2 7 4
0
1 5
0
4
Hint
- For of the testdata, , , .
- For of the testdata, , , .
- For of the testdata, , , . It is guaranteed that initially, for every node, the sum of its number of blossoms and its number of children is greater than and does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号