Time Limit:10 Sec Memory limit:128 MB
submit:2937 solved:1536 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
L i
, as well as paid toThe Ninja's salary budget
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 < I Ninja's superior number;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 i
each indicates that the first
I
a Ninja's superiors, salary and leadership.
Master
Meet
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 Input
5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1
Sample Output6
left-leaning tree/Can and heapsubconsciously wrote and looked at the set, but it was no use. 47 rows del function did not pass arguments, re 3 times only to find.
1 /*by Silvern*/2#include <algorithm>3#include <iostream>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8 #defineLL Long Long9 using namespacestd;Ten Const intmxn=100010; One intRead () { A 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 ();} the returnx*F; - } - structedge{ - intv,nxt; +}e[mxn<<1]; - inthd[mxn],mct=0; + voidAdd_edge (intUintv) { AE[++MCT].V=V;E[MCT].NXT=HD[U];HD[U]=MCT;return; at } - structninja{intFA; LL C,le;} A[MXN]; - structnode{ - intL,r; - LL W; - }T[MXN]; in // - intN; LL m; to //can and Heap + intfa[mxn],rt[mxn],size[mxn],cnt; - LL SUM[MXN]; the intFindintx) { * if(fa[x]==x)returnx; $ returnfa[x]=find (Fa[x]);Panax Notoginseng } - intDEP[MXN]; the intMgeintXinty) { + if(x*y==0)returnx+y; A if(T[X].W<T[Y].W) {Swap (x, y);}//Dagen theT[x].r=mge (t[x].r,y); + if(dep[t[x].l]<DEP[T[X].R]) swap (T[X].L,T[X].R); -dep[x]=dep[t[x].r]+1; $ returnx; $ } - voidDelint&X) {// -x=mge (T[X].L,T[X].R); the //Fa[fa[x]]=fa[x]; - return;Wuyi } theInline LL Top (intx) { - //int X=find (x); Wu returnT[X].W; - } About // $LL ans=0; - voidDFS (intu) { -rt[u]=++CNT; -t[cnt].w=a[u].c; Asum[u]=a[u].c; +size[u]=1; the for(intI=hd[u];i;i=e[i].nxt) { - intv=e[i].v; $ DFS (v); thesum[u]+=Sum[v]; thesize[u]+=Size[v]; thert[u]=mge (Rt[u],rt[v]); the } - while(sum[u]>m) {//sum less than m in maintenance heap insum[u]-=Top (Rt[u]); theDel (Rt[u]); size[u]--; the } AboutAns=max (ans, (LL) size[u]*a[u].le); the return; the } the intMain () { + inti,j; -N=read (); m=read (); the for(i=1; i<=n;i++){BayiA[i].fa=read (); theA[i].c=read (); thea[i].le=read (); - Add_edge (a[i].fa,i); - } theDFS (1); theprintf"%lld\n", ans); the return 0; the}
Bzoj2809 [apio2012]dispatching