Title: Given a tree, select some points in a subtrees tree, salary and not exceed m, the number of points * The maximum of leadership capacity of a sub-root node
Consider for each node, we maintain a data structure in which greed looks for small payroll hires.
Each node violence reconstruction must not be, we consider the data structure, each node will directly merge the information of the child node
Treap can be combined with heuristics, or can be used with a heap
Today deliberately to learn this play should be 0.0 first wrote the left-leaning tree and then wrote the next random heap ... The latter is faster, but it's recommended to start with the left-leaning tree.
In a word, the balance tree constant is all about 0.0.
treap+ heuristic Merging
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 100100using namespace Std;typedef Long long ll;struct abcd{abcd *ls,*rs;int key;int cnt,siz;ll num,sum;abcd (ll X,int y); v OID maintain ();} *null=new ABCD (0,0), *tree[m];struct edge{int To,next;} Table[m];int head[m],tot;int n,root;ll m,ans,leadership[m];void Add (int x,int y) {table[++tot].to=y;table[tot].next= Head[x];head[x]=tot;} ABCD:: ABCD (ll X,int y) {ls=rs=null;sum=x*y;num=x;cnt=siz=y;key=y?rand (): 0;} void ABCD:: Maintain () {siz=ls->siz+rs->siz+cnt;sum=ls->sum+rs->sum+num*cnt;} void Zig (ABCD *&x) {ABCD *y=x->ls;x->ls=y->rs;y->rs=x;x=y;x->rs->maintain ();} void Zag (ABCD *&x) {ABCD *y=x->rs;x->rs=y->ls;y->ls=x;x=y;x->ls->maintain ();} void Insert (ABCD *&x,ll y,int z) {if (x==null) {x=new abcd (y,z); return;} if (y==x->num) X->cnt+=z;else if (y<x->num) {Insert (x->ls,y,z), if (X->ls->key>x->key) Zig ( x);} Else{insert (x->RS,Y,Z); if (X->rs->key>x->key) Zag (x);} X->maintain ();} int query (ABCD *x,ll y) {if (x==null) return 0;ll temp=x->ls->sum;int re=0;if (y<=temp) return Query (x->ls,y); Re+=x->ls->siz;y-=temp;if (y<=x->num*x->cnt) return re+y/x->num;re+=x->cnt;y-=x->num*x- >cnt;return re+query (x->rs,y);} void decomposition (abcd *&x,int y) {if (x==null) return;D ecomposition (x->ls,y);D ecomposition (x->rs,y); Nsert (tree[y],x->num,x->cnt);d elete X;x=null;} void tree_dp (int x) {int i;for (i=head[x];i;i=table[i].next) {TREE_DP (table[i].to); if (tree[x]->siz<tree[table[ I].to]->siz) Swap (tree[x],tree[table[i].to]);D ecomposition (tree[table[i].to],x);} Ans=max (Ans,leadership[x]*query (Tree[x],m));} int main () {//freopen ("2809.in", "R", stdin),//freopen ("2809.out", "w", stdout); int I,fa;ll x;cin>>n>>m; for (i=1;i<=n;i++) {scanf ("%d%lld%lld", &fa,&x,&leadership[i]), if (!FA) Root=i;else Add (fa,i); Tree[i] =new ABCD (x,1);} TREE_DP (root); cout<<aNs<<endl;} lld!!
Left-leaning tree
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 100100using namespace Std;struct abcd{abcd *ls,*rs;int num,h;abcd (int x);} *null=new ABCD (0), *tree[m];struct edge{int To,next;} Table[m];int head[m],tot;int N,m,root,leadership[m],sum[m],size[m];long long ans;void Add (int x,int y) {Table[++tot]. To=y;table[tot].next=head[x];head[x]=tot;} ABCD:: ABCD (int x) {ls=rs=null;num=x;if (x) H=0;else h=-1;} abcd* Merge (ABCD *x,abcd *y) {if (x==null) return y;if (Y==null) return x;if (x->num<y->num) swap (x, y); x->rs= Merge (X->rs,y), if (x->ls->h<x->rs->h) swap (X->LS,X->RS); X->h=x->rs->h+1;return x ;} void tree_dp (int x) {int i;for (i=head[x];i;i=table[i].next) {TREE_DP (table[i].to); Tree[x]=merge (tree[x],tree[table[ I].to]); Sum[x]+=sum[table[i].to];size[x]+=size[table[i].to];while (sum[x]>m) {sum[x]-=tree[x]->num;--size[ X];tree[x]=merge (Tree[x]->ls,tree[x]->rs);}} Ans=max (ans, (long Long) size[x]*leadership[x]);} IntMain () {int i,fa,x;cin>>n>>m;for (i=1;i<=n;i++) {scanf ("%d%d%d", &fa,&x,&leadership[i]); if (!FA) Root=i;else Add (fa,i); tree[i]=new abcd (x); sum[i]=x;size[i]=1;} TREE_DP (root); Cout<<ans<<endl;}
Random Heap
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 100100using namespace Std;struct abcd{abcd *ls,*rs;int num;abcd (int x);} *null=new ABCD (0), *tree[m];struct edge{int To,next;} Table[m];bool son;int head[m],tot;int N,m,root,leadership[m],sum[m],size[m];long long ans;void Add (int x,int y) {table[ ++tot].to=y;table[tot].next=head[x];head[x]=tot;} ABCD:: ABCD (int x) {ls=rs=null;num=x;} abcd* Merge (ABCD *x,abcd *y) {if (x==null) return y;if (Y==null) return x;if (x->num<y->num) swap (x, y); if (son^=1) X->rs=merge (x->rs,y); Elsex->ls=merge (x->ls,y); return x;} void tree_dp (int x) {int i;for (i=head[x];i;i=table[i].next) {TREE_DP (table[i].to); Tree[x]=merge (tree[x],tree[table[ I].to]); Sum[x]+=sum[table[i].to];size[x]+=size[table[i].to];while (sum[x]>m) {sum[x]-=tree[x]->num;--size[ X];tree[x]=merge (Tree[x]->ls,tree[x]->rs);}} Ans=max (ans, (long Long) size[x]*leadership[x]);} int main () {int i,fa,x;cin>>n>>m;for (i=1;i<=n;i+ +) {scanf ("%d%d%d", &fa,&x,&leadership[i]), if (!FA) Root=i;else Add (fa,i); tree[i]=new abcd (x); sum[i]=x; Size[i]=1;} TREE_DP (root); Cout<<ans<<endl;}
Bzoj 2809 APIO2012 dispatching treap+ heuristic merge/Can and Heap