bzoj2809 [Apio2012]dispatching

来源:互联网 发布:医生收入 知乎 编辑:程序博客网 时间:2024/06/16 06:43

传送门

忍者的关系是树形结构,所以一个节点只会对其祖先造成影响,所以我们可以在dfs的时候进行维护。
满意度的定义是派遣的忍者总数乘管理者的领导力水平,其中领导力水平就是我们dfs到的点的一个属性,所以我们只需要维护所能得到的最大忍者总数就好了。由于派遣忍者需要费用,所以我们可以贪心地在费用超出预算的时候扔掉费用最大的忍者,这个可以用堆解决;但是对于dfs到的节点,我们要合并其所有儿子的堆,所以要使用可并堆(左偏树或者斜堆等等)维护。
注意累加费用的时候可能会爆int,所以要将相关的改成long long。我的1A就这么没了桑心

CODE:

#include<cstdio>#include<iostream>using namespace std;typedef long long ll;const int N=1e6+10;struct node{    int dis,id,size;    ll num,cost;    node *ch[2];}pool[N],*t[N],*null;struct edge{    int nxt,to;}a[N];int head[N],lv[N];int n,x,tot,num,root;ll m,ans,y;inline void readint(int &n){    n=0;char c=getchar();    while(c<'0'||c>'9') c=getchar();    while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();}inline void readll(ll &n){    n=0;char c=getchar();    while(c<'0'||c>'9') c=getchar();    while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();}inline ll max(const ll &a,const ll &b){return a>b?a:b;}inline void add(int x,int y){    a[++num].nxt=head[x],a[num].to=y,head[x]=num;}inline void getnew(ll cost,int id){    node *now=pool+ ++tot;    now->ch[0]=now->ch[1]=null;    now->num=now->cost=cost,now->id=id;    now->dis=now->size=1;    t[id]=now;}node *merge(node *x,node *y){    if(x==null) return y;    if(y==null) return x;    if(x->num<y->num||(x->num==y->num&&x->id>y->id)) swap(x,y);    x->ch[1]=merge(x->ch[1],y);    if(x->ch[0]->dis<x->ch[1]->dis) swap(x->ch[0],x->ch[1]);    x->dis=x->ch[1]->dis+1;    x->cost=x->ch[0]->cost+x->ch[1]->cost+x->num;    x->size=x->ch[0]->size+x->ch[1]->size+1;    return x;}node *dfs(int now){    node *root=t[now];    for(int i=head[now];i;i=a[i].nxt)      root=merge(root,dfs(a[i].to));    while(root->cost>m) root=merge(root->ch[0],root->ch[1]);    ans=max(ans,1ll*root->size*lv[now]);    return root;}int main(){    null=pool;    null->ch[0]=null->ch[1]=null;    null->dis=-1;    readint(n),readll(m);    for(int i=1;i<=n;i++)    {        readint(x),readll(y),readint(lv[i]),getnew(y,i);        if(x) add(x,i);        else root=i;    }    dfs(root);    printf("%lld",ans);    return 0;}
原创粉丝点击