Bzoj2809[apio2012]dispatching
Test instructions
N points make up a tree, each point has a leadership and cost, you can make a point when the leader, and then in this point of the sub-tree select some cost and not more than M points, get leadership leadership by the number of points selected (leader can not be selected) profit. The maximum value of the profit. n≤100000
Exercises
can be stacked. Can get a conclusion, that is, in the sub-tree in the selection point, the first choice of all points, if the cost is super, the cost of the biggest culling, know that the cost is not super, so the number of selected options, the process can be maintained with the heap. At the same time, the heap of each point can be merged by the heap of the child tree, so it needs to be stacked.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 1001006 #definell Long Long7 using namespacestd;8 9 intch[maxn][2],rt[maxn],n,m,st; ll SZ[MAXN],V[MAXN],ANS,SM[MAXN],GD[MAXN];Ten structe{intT,n;}; e ES[MAXN];intESS,G[MAXN]; One voidPeintFintT) {es[++ess]= (e) {t,g[f]}; g[f]=ess;} A voidInit () {ess=0; Memset (G,0,sizeof(g));} - voidUpdateintx) {sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1; sm[x]=sm[ch[x][0]]+sm[ch[x][1]]+v[x];} - intMergeintXinty) { the if(!x| |! YreturnX+y;if(V[x]<v[y]) swap (x, y); ch[x][1]=merge (ch[x][1],y); -Swap (ch[x][0],ch[x][1]); Update (x);returnx; - } - voidPopint&x) { + intY=merge (ch[x][0],ch[x][1]); ch[x][0]=ch[x][1]=0; sz[x]=1; SM[X]=V[X]; x=y; - } + voidDfsintx) { ARt[x]=x; for(intI=G[X];I;I=ES[I].N) DFS (es[i].t), rt[x]=merge (rt[x],rt[es[i].t]); at while(sz[rt[x]]&&sm[rt[x]]>m) Pop (rt[x]); Ans=max (ans,sz[rt[x]]*gd[x]); - } - intMain () { -scanf"%d%d",&n,&m); Init (); -Inc (I,1, N) { - intA ll B,c; scanf"%d%lld%lld", &a,&b,&c);if(a) PE (A,I);Elsest=i; inV[i]=sm[i]=b; sz[i]=1; Gd[i]=c; ch[i][0]=ch[i][1]=rt[i]=0; - } toans=0; DFS (ST); printf"%lld", ans);return 0; +}
20160531
Bzoj2809[apio2012]dispatching