Time Limit: 10 sec memory limit: 256 MB
Description
A long time ago, a group of rabbits lived in the forest. One day, the rabbits suddenly decided to see cherry blossoms. The cherry trees in the forest where rabbits are located are very special. The cherry blossom tree consists of \ (n \) branches and forks. The number ranges from \ (0 \) to \ (n-1 \). This \ (n \) branches are connected by \ (n-1 \). We can regard it as a root tree structure, where node \ (0 \) is the root node. Each node of this tree has some cherry blossoms, among which \ (I \) has \ (C_ I \) cherry blossoms. Each node of the cherry blossom tree has the maximum load \ (M \). For each node \ (I \), the sum of the number of its son nodes and the number of cherry blossoms on the I node cannot exceed \ (M \), that is, \ (son (I) + C_ I \ Leq m \), here, \ (son (I) \) indicates the number of sons of \ (I \). If \ (I \) is a leaf node, \ (son (I) = 0 \)
Now the rabbits think that there are too many nodes on the cherry tree and they want to remove some nodes. After a node is removed, the cherry blossom and its son nodes on the node are connected to the parent node of the deleted node. If the parent node is also deleted, the connection continues 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 included in the load.
Input
Enter two positive integers in the first line. \ (n \) and \ (M \) indicate the number of nodes and the maximum load respectively.
The second line \ (n \) integer \ (C_ I \) indicates the number of cherry blossoms on the \ (I \) node
Next \ (n \), the first number of each line \ (K_ I \) indicates the number of sons of this node, and the next \ (K_ I \) integer indicates the number of sons of this node
Output
An integer in one row indicates the maximum number of nodes that can be deleted.
Sample Input
10 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 \ Leq n \ Leq 2000000 \), \ (1 \ Leq m \ Leq 100000 \), \ (0 \ Leq C_ I \ Leq 1000 \)
Data guarantee at the beginning, the sum of the cherry blossom count and the number of son nodes on each node is greater than \ (0 \) and cannot exceed \ (M \)
Solution
Our weight is clearly the number of cherry blossoms + number of subnodes.
We should try to delete the most vertices now.
For a vertex \ (x \), the number of points that can be deleted in all its subnodes is obviously irrelevant to \ (x. Now, we only care about whether each of its subnodes can be deleted.
So we can create a new array \ (C [I] \) to indicate the weight when \ (I \) gets the most points.
Now, we sort all the subnodes in ascending order of \ (C \) values, and select a smaller vertex to delete. Why is it true? You can think about it like this. If there is a relatively large weight point that is not selected, then in fact we will only lose one contribution of that subnode. However, if we select a smaller weight, it is possible that the root node of the entire subtree cannot be selected from its father (this will only lose one contribution ), it is more likely that you can choose from your father (so it is good to make no contribution), so it is certainly not bad to give priority to small choices.
Therefore, we are now taking the priority of selecting a point with a small value of \ (C \) to obtain the optimal solution.
#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>using namespace std;#define lowbit(x) ((x)&(-(x)))#define REP(i,a,n) for(register int i=(a);i<=(n);++i)#define PER(i,a,n) for(register int i=(a);i>=(n);--i)#define FEC(i,x) for(register int i=head[x];i;i=g[i].ne)#define dbg(...) fprintf(stderr,__VA_ARGS__)namespace io{ const int SIZE=(1<<21)+1;char ibuf[SIZE],*iS,*iT,obuf[SIZE],*oS=obuf,*oT=oS+SIZE-1,c,qu[55];int f,qr; #define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SIZE,stdin),(iS==iT?EOF:*iS++)):*iS++) inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;} inline int getc(){return gc();} inline void putc(char x){*oS++=x;if(oS==oT)flush();} template<class I>inline void read(I &x){for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;} template<class I>inline void write(I x){if(!x)putc('0');if(x<0)putc('-'),x=-x;while(x)qu[++qr]=x%10+'0',x/=10;while(qr)putc(qu[qr--]);} inline void print(const char *s){while(*s!='\0')putc(*s++);} inline void scan(char *s){for(c=gc();c<=' ';c=gc());for(;c>' ';c=gc())*(s++)=c;*s='\0';} struct Flusher_{~Flusher_(){flush();}}io_flusher_;}//orz laofudasuanusing io::read;using io::putc;using io::write;using io::print;typedef long long ll;typedef unsigned long long ull;template<typename A,typename B>inline bool SMAX(A&x,const B&y){return x<y?x=y,1:0;}template<typename A,typename B>inline bool SMIN(A&x,const B&y){return y<x?x=y,1:0;}const int N=2000000+7;int n,m,x,y,c[N],ans;vector<int>g[N];inline char cmp(const int&x,const int&y){return c[x]<c[y];}inline void DFS(int x){ int len=g[x].size(),&cnt=c[x]; for(register int i=0;i<len;++i)DFS(g[x][i]); sort(g[x].begin(),g[x].end(),cmp); for(register int i=0;i<len;++i)if(cnt+c[g[x][i]]-1<=m){cnt+=c[g[x][i]]-1;++ans;}else break;}int main(){#ifndef ONLINE_JUDGE freopen("BZOJ4027.in","r",stdin);freopen("BZOJ4027.out","w",stdout);#endif read(n),read(m); for(register int i=1;i<=n;++i)read(c[i]); for(register int i=1;i<=n;++i){ read(x);c[i]+=x; for(register int j=1;j<=x;++j)read(y),g[i].push_back(y+1); } DFS(1);write(ans),putc('\n');}
Heoi2015 rabbit and cherry blossom