bzoj4027 贪心

来源:互联网 发布:反淘宝联盟ebrun 编辑:程序博客网 时间:2024/06/05 23:46

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=4027

大意:给一棵有根树,当你删除一个节点时,这个点上的权值会加到父节点,儿子连到父节点上。要求一个点上的权值+儿子个数<=m。根节点不能删。

贪心:设删点对父节点的影响为g。对一个子树,肯定尽可能多的删子树内的点。(删子树根节点的话只能删一个点且最后会删到根节点)

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct sef { int ne,en; }h[2000005];int tot,first[2000005];int q[2000005],ql;int ans,n,V[2000005],m;inline void setl(int x,int y){    h[++tot].ne=first[x]; h[tot].en=y;    first[x]=tot;}void Go(int x){    int y;    int ql=0;    for (int i=first[x];y=h[i].en,i;i=h[i].ne) {        Go(y);        ++V[x];    }    for (int i=first[x];y=h[i].en,i;i=h[i].ne) q[++ql]=V[y]-1;    sort(q+1,q+ql+1);    for (int i=1;i<=ql;++i)        if (V[x]+q[i]<=m) V[x]+=q[i],++ans;        else break;}int main(){    scanf("%d%d",&n,&m);    for  (int i=1;i<=n;++i)         scanf("%d",V+i);    int k,x;    for (int i=1;i<=n;++i) {        scanf("%d",&k);        while (k--) {            scanf("%d",&x);            setl(i,++x);        }    }    ans=0;    Go(1);    printf("%d\n",ans);}
1 0
原创粉丝点击