觉得是贪心,但是一开始不太肯定。。。然后就A了

一个点对它的父亲的贡献就是自己的权值加儿子的个数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int read(){
 4   int x=0,f=1;char ch=getchar();
 5   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 6   while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 7   return x*f;
 8 }
 9 #define N 2000005
10 int n,m,v[N],son[N],ans=0;
11 struct Node{
12   int to,next;
13 }e[N];
14 int tot,head[N];
15 void add(int x,int y){
16   e[++tot]=(Node){y,head[x]};head[x]=tot;
17 }
18 int dfs(int x){
19   if(!head[x])return v[x];
20   int top=0,q[son[x]];
21   for(int i=head[x];i;i=e[i].next){
22     q[top++]=dfs(e[i].to);
23   }
24   sort(q,q+top);
25   for(int i=0;i<top;i++)if(v[x]+q[i]+son[x]-1<=m)son[x]--,v[x]+=q[i],ans++;
26   return v[x]+son[x];
27 }
28 int main(){
29   n=read();m=read();
30   for(int i=1;i<=n;i++)v[i]=read();
31   for(int i=1;i<=n;i++){
32     son[i]=read();
33     for(int j=1;j<=son[i];j++){
34       int x=read()+1;
35       add(i,x);
36     }
37   }
38   dfs(1);
39   printf("%d\n",ans);
40 }
View Code

相关文章: