Description
A long long time ago, there lived a group of rabbits in the forest. One day, the rabbits suddenly decided to go to see the cherry blossoms. The cherry trees in the forest where the rabbits are located are very special. The cherry tree consists of n branches, numbered from 0 to n-1, the N fork points are connected by n-1 branches, we can think of it as a root tree structure, where node No. 0 is the root node. There will be some cherry blossoms on each node of the tree, and the node I has c_i cherry blossoms. Each node of the Cherry tree has the largest load m, for each node I, its son number of nodes and the number of cherry blossoms on the I node cannot exceed M, that is son (i) + c_i <= m, where son (i) represents the number of sons of I, if I is a leaf node, then son (i) = 0
Now the rabbits think there are too many nodes on the cherry tree, hoping to get rid of some of them. When a node is removed, the cherry blossom on the node and its son node are connected to the deleted node's parent node. If the parent node is also deleted, it will continue to connect up until the first node is not deleted. Now the rabbits want to calculate the maximum number of nodes that can be deleted without violating the maximum load. Note that the root node cannot be deleted and the deleted node is not counted into the payload. Input
The first line enters two positive integers, and N and M represent the number of nodes and the maximum load
The second row n integer c_i, which represents the number of cherry blossoms on the first node, next n rows, the number of k_i for each row, and the next k_i an integer representing the number of the node's son. Output
An integer line representing the maximum number of nodes that can be deleted.
Sample Input
Ten 40 2 2 2 4 1 0 4 1 13 6 2 31 91 81 1002 7 401 50
Sample Output
4
HINT
For 100% data, 1 <= n <= 2000000, 1 <= m <= 100000, 0 <= c_i <= 1000
Data guaranteed initially, the sum of the number of cherry blossoms per node and the number of sons nodes is greater than 0 and does not exceed msolution
Greedy, each time choose (direct son number + subtree Sakura number) youngest son, until no longer selectable
#include <vector>#include<cstdio>#include<algorithm>using namespacestd;Const intn=2000010;intN,m,ans;structdt{intx; Vector<int>e;} A[n];BOOL_CMP (intPintq) { returna[p].x<a[q].x;}voidDyn_pro (intx) { if(A[x].e.empty ())return; for(intI=0; I<a[x].e.size (); i++) Dyn_pro (A[x].e[i]); Sort (A[x].e.begin (), A[x].e.end (), _cmp); for(intI=0; I<a[x].e.size (); i++){ inttmp=a[a[x].e[i]].x-1; if(a[x].x+tmp<=m) a[x].x+=tmp, ans++; Else Break; }}intMain () {scanf ("%d%d",&n,&m); for(intI=0; i<n;i++) scanf ("%d",&a[i].x); for(intI=0; i<n;i++){ intNum,son; scanf ("%d",&num); a[i].x+=num; for(intj=0; j<num;j++) scanf ("%d",&son), A[i].e.push_back (son); } Dyn_pro (0); printf ("%d\n", ans);}
[bzoj4027] [HEOI2015] [Rabbits and cherry blossoms] (Tree-shaped DP idea + metaphysical greed)