树上贪心问题……跟APIO2015练习赛的C很像啊……

  我的思路是:从叶子向上考虑,令a[x]表示x这个节点上樱花数量与儿子个数的和(即对于任意的x,都有$a[x]\leq m$)每次从儿子的a值中贪心地选最小的加到当前节点中(当然还要-1),然后就不用管了……因为如果某个儿子不能删去,将后代并入父亲,那么之后也不可能在将父亲删去后,再将这个儿子删去,因为越向上樱花累积的越多,而且这样删过以后儿子也是会加上去的……呃总之就是有这么个性质吧。。。描述的不是很清楚请见谅。

  所以就可以贪心了……(感觉什么也没说)

  然而蒟蒻用的是vector……然后时空复杂度高的飞起……QAQ求老司机教正确姿势

 1 /**************************************************************
 2     Problem: 4027
 3     User: Tunix
 4     Language: C++
 5     Result: Accepted
 6     Time:5096 ms
 7     Memory:105224 kb
 8 ****************************************************************/
 9  
10 //BZOJ 4027
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 typedef long long LL;
23 inline int getint(){
24     int r=1,v=0; char ch=getchar();
25     for(;!isdigit(ch);ch=getchar()) if(ch=='-')r=-1;
26     for(; isdigit(ch);ch=getchar()) v=v*10+ch-'0';
27     return r*v;
28 }
29 const int N=2000010;
30 /*******************template********************/
31 int n,m,c[N],a[N],ans;
32 vector<int>G[N],V[N];
33 void dfs(int x){
34     a[x]=c[x];
35     rep(i,G[x].size()){
36         dfs(G[x][i]);
37         a[x]++;
38         V[x].pb(a[G[x][i]]);
39     }
40     if (V[x].size()) sort(V[x].begin(),V[x].end());
41     rep(i,V[x].size()){
42         if (a[x]+V[x][i]-1>m) break;
43         a[x]=a[x]+V[x][i]-1;
44         ans++;
45     }
46 }
47 int main(){
48 #ifndef ONLINE_JUDGE
49     freopen("4027.in","r",stdin);
50     freopen("4027.out","w",stdout);
51 #endif
52     n=getint(); m=getint();
53     rep(i,n) c[i]=getint();
54     rep(i,n){
55         int x=getint();
56         F(j,1,x){
57             int y=getint();
58             G[i].pb(y);
59         }
60     }
61     dfs(0);
62     printf("%d\n",ans);
63     return 0;
64 }
View Code

相关文章: