I think it's greedy, but I'm not sure at first ... And then it's a.
A point to its father's contribution is his own weight plus the number of sons
1#include <bits/stdc++.h>2 using namespacestd;3 intRead () {4 intx=0, f=1;CharCh=GetChar ();5 while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}6 while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();}7 returnx*F;8 }9 #defineN 2000005Ten intn,m,v[n],son[n],ans=0; One structnode{ A intTo,next; - }e[n]; - intTot,head[n]; the voidAddintXinty) { -E[++tot]= (Node) {y,head[x]};head[x]=tot; - } - intDfsintx) { + if(!head[x])returnV[x]; - inttop=0, Q[son[x]]; + for(intI=head[x];i;i=E[i].next) { Aq[top++]=DFS (e[i].to); at } -Sort (q,q+top); - for(intI=0; i<top;i++)if(v[x]+q[i]+son[x]-1<=m) son[x]--, v[x]+=q[i],ans++; - returnv[x]+Son[x]; - } - intMain () { inN=read (); m=read (); - for(intI=1; i<=n;i++) v[i]=read (); to for(intI=1; i<=n;i++){ +son[i]=read (); - for(intj=1; j<=son[i];j++){ the intX=read () +1; * Add (i,x); $ }Panax Notoginseng } -Dfs1); theprintf"%d\n", ans); +}
View Code 4027: [HEOI2015] rabbit with cherry blossom time limit:10 Sec Memory limit:256 MB
submit:713 solved:410
[Submit] [Status] [Discuss] 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 Input10 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
0Sample Output4HINT
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 no more than M
BZOJ4027: [HEOI2015] rabbit and cherry blossom greedy