2809: [apio2012]dispatching time limit:10 Sec Memory limit:128 MB
submit:2334 solved:1192
[Submit] [Status] [Discuss] Description in a ninja gang, some ninjas are selected to be dispatched to the customer and rewarded for their work. In this gang, a ninja is called Master. In addition to master, each ninja has and has only one parent. To keep it private and to enhance the leadership of the Ninja, all instructions related to their work are always sent by the superior to his direct subordinates, and not by other means. Now you need to recruit a bunch of ninjas and send them to customers. You need to pay a certain salary for each ninja you send, and make the total amount you pay not exceed your budget. In addition, in order to send instructions, you need to select a ninja as the manager, ask the manager to send instructions to all the sent ninja, when sending instructions, any ninja (whether or not dispatched) can be the message of the sender. Managers themselves can be dispatched, or they may not be dispatched. Of course, if the manager is not being removed, there is no need to pay the manager's salary. Your goal is to make the most of your customers ' satisfaction on budget. This defines the customer's satisfaction as the total number of ninjas dispatched is multiplied by the manager's leadership level, and each ninja's leadership level is also certain. Write a program, given every ninja
IThe Superior
B
I
,Salary
Ci
,Leadership Power
L i
,And the total salary paid to the ninjas.
M, the maximum value of customer satisfaction is output in the budget meeting the above requirements. 1≤
NThe number of ≤100,000 ninjas; 1≤
M≤1,000,000,000 salary total budget; 0≤
B
I < iThe number of the Ninja's superiors; 1≤
C
i≤mThe salary of a ninja; 1≤.
L
i≤1,000,000,000Ninja's leadership level. Input reads data from the standard input. The first line consists of two integers
NAnd
M, where
NIndicates the number of ninjas,
MRepresents the total budget of a salary. Next
NThe line describes the ninja's superiors, salary, and leadership. The first of these
IRow contains three whole
B
I, C i, L iThe superior, the salary and the leadership of the first ninja, respectively.
.
MasterMeet
B i = 0
,And each ninja's boss's number must be less than his own number
B
I < i
. Output outputs a number that represents the maximum value of customer satisfaction in the budget. Sample Input5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1
Sample Output6
HINT
If we choose a ninja numbered 1 as a manager and dispatch a third and fourth Ninja, the sum of the salaries is 4, not exceeding the total budget of 4. Since 2 ninjas were dispatched and the manager's leadership was 3,
User satisfaction is 6, is the maximum number of user satisfaction can be obtained.
Source Solution
splay+ heuristic Merging
The title may be a little understood, but I didn't understand it at first.
Academic point is that: given a tree, each point has a C value and an L value, a point u and a point set S, so that the points inside the s are in the root of the lesson of the tree, s inside the C value of all points and less than equal to M, and L[u] * | s| Max.
So think more, first of all, the sum of the salary is limited, then it is clear that to make the number of the dispatch of the most, a subtrees tree, the smallest few
Then use splay to maintain, maintain sum, use C to help find and insert;
Because the superior number must be less than subordinate, from n~1 reverse merger can.
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 110010structdata{intNext,to;} EDGE[MAXN];inthead[maxn],cnt;voidAddintUintV) {cnt++;edge[cnt].next=head[u];head[u]=cnt;edge[cnt].to=v;}intN,m,q;intQUE[MAXN];intRoot,sz;intfa[maxn],son[maxn][2],LI[MAXN],CI[MAXN],SIZE[MAXN];Long LongSUMC[MAXN];int Get(intNow) {returnson[fa[now]][1]==Now ;}voidUpdateintNow ) { if(!now)return; Size[now]=1+size[son[now][0]]+size[son[now][1]]; Sumc[now]=sumc[son[now][0]]+sumc[son[now][1]]+Ci[now];}voidRotateint&Now ) { intOld=fa[now],oldf=fa[old],which=Get(now); Son[old][which]=son[now][which^1]; fa[son[old][which]]=Old ; Fa[old]=now; son[now][which^1]=old; fa[now]=Oldf; if(OLDF) son[oldf][son[oldf][1]==old]=Now ; Update (old); Update (now);}voidSplay (int&Now ) { for(intF (f=Fa[now]); Rotate (now))if(Fa[f])if(Get(now) = =Get(f) Rotate (f); Elserotate (now);}voidInsertintXintRT) { inty=rt,last=0; while(y) { last=y; if(Ci[x]>ci[y]) y=son[y][1]; Elsey=son[y][0]; } Fa[x]=last; son[last][(Ci[x]>ci[last])]=x; Update (x); Update (last); Splay (x);}voidMergeintUintv) {splay (U); splay (v); if(size[u]>Size[v]) swap (U,V); intHe=0, Ta=1, last=v; que[1]=u; while(he<ta) { intx = que[++He]; if(son[x][0]) que[++ta]=son[x][0]; if(son[x][1]) que[++ta]=son[x][1]; son[x][0]=son[x][1]=0; } for(intI=1; i<=ta; i++) Insert (que[i],last), last=que[i];}intAskintRtintk) { if(!RT)return 0; if(sumc[son[rt][0]]+CI[RT]==K)returnsize[son[rt][0]]+1; if(sumc[rt]<=k)returnSize[rt]; if(sumc[son[rt][0]]+CI[RT]>K)returnAsk (son[rt][0],k); Else returnsize[son[rt][0]]+1+ask (son[rt][1],k-(sumc[son[rt][0]]+Ci[rt]));}intMain () {n=read (), m=read (); for(intI=1; i<=n; i++) { intX=read (); Ci[i]=read (); li[i]=read (); Sumc[i]=ci[i]; size[i]=1; if(!x) root=x;ElseAdd (x,i); } Long Longans=0; for(intI=n; I>0; i--) { for(intJ=head[i]; J j=edge[j].next) Merge (Edge[j].to,i); Splay (i); Ans=max (ans, (Long Long) li[i]*Ask (i,m)); } printf ("%lld\n", ans); return 0;}
Reason, the first t fell ... Tuned for a while. Find yourself insert a little problem, modified a bit, delete debugging information when more deleted a sentence ... I've had a lot of time.
"BZOJ-2809" dispatching dispatch splay + heuristic merge