#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 nn branch fork points, numbered from 00 to n1n-1. These nn fork points are connected by n1n-1 branches, and we can regard it as a rooted tree where node 00 is the root.

Each node of the tree has some cherry blossoms; specifically, node ii has cic_i blossoms. Every node of the sakura tree has a maximum load mm. For each node ii, the sum of the number of its children and the number of blossoms on node ii must not exceed mm, i.e., son(i)+cimson(i) + c_i \leq m, where son(i)son(i) denotes the number of children of ii. If ii is a leaf, then son(i)=0son(i) = 0.

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, nn and mm, denoting the number of nodes and the maximum load.

The second line contains nn integers cic_i, where cic_i is the number of blossoms on node ii.

Then follow nn lines. In each line, the first number kik_i is the number of children of this node, followed by kik_i 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 30%30\% of the testdata, n5×103n \leq 5 \times 10^3, m100m \leq 100, ci100c_i \leq 100.
  • For 70%70\% of the testdata, n2×105n \leq 2 \times 10^5, m2×103m \leq 2 \times 10^3, ci103c_i \leq 10^3.
  • For 100%100\% of the testdata, 1n2×1061 \leq n \leq 2 \times 10^6, 1m1051 \leq m \leq 10^5, 0ci1030 \leq c_i \leq 10^3. It is guaranteed that initially, for every node, the sum of its number of blossoms and its number of children is greater than 00 and does not exceed mm.

Translated by ChatGPT 5