DescriptionIn a ninja gang, some ninjas are chosen to send to the customer, and then according to their workGet compensation.In this gang, a ninja is calledMaster。 Apart fromMasterOutside, each ninjaAll have and have only one superior. To keep the Ninja's leadership at the same time, all relevant to their workInstructions are always sent by the superior to his direct subordinates, 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 has not beenThere 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 each ninja
I 's superior
B
i
, salary
Ci
, leadership
L i
, as well as paid to the Ninja's salary budget
M, which outputs the maximum of customer satisfaction when the budget meets the above requirements.
1 ≤
N ≤100,000 number of ninjas; 1 ≤
M ≤1,000,000,000 Salary total budget; 0 ≤
B
i < i Ninja's superior number; 1 ≤
C
i≤m Ninja's salary; 1 ≤
L
i≤1,000,000,000 Ninja's leadership level.
Inputreads data from standard input. The first line contains two integers
n and
m, where
n represents the number of ninjas, and
m represents the total pre-calculation of the salary . Next
N I Row contains three whole
b
i, C I, L i
represents the
i
master
meet
b i = 0
B
Outputoutputs 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 1Sample 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 2, is the maximum number of user satisfaction can be obtained.
Lazy to write treap heuristic merge, wrote a DFS sequence + chairman Tree, pay attention to duplicate weights.
#include <cstdio>#include<cctype>#include<cstring>#include<algorithm>#defineRep (s,t) for (int i=s;i<=t;i++)using namespacestd;intRead () {intx=0, f=1;CharC=GetChar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=100010;Const intMaxnode=4000010; typedefLong LongLL;intn,m,rt,first[maxn],next[maxn],to[maxn],e;voidAddedge (intUintv) {if(!u) rt=v; to[++e]=v;next[e]=first[u];first[u]=e;}ints[maxnode],ls[maxnode],rs[maxnode],l[maxn],r[maxn],pos[maxn],sv[maxn],lv[maxn],cnt; LL Sumv[maxnode],ans;voidDfsintx) {l[x]=++cnt;pos[cnt]=x; for(intI=first[x];i;i=Next[i]) DFS (to[i]); R[X]=CNT;}intRoot[maxn],tot;voidBuildint& Y,intXintLintRintPOS) {Sumv[y=++tot]=sumv[x]+pos;s[y]=s[x]+1; if(L==R)return; intMid=l+r>>1; ls[y]=ls[x];rs[y]=Rs[x]; if(pos<=mid) build (Ls[y],ls[x],l,mid,pos); ElseBuild (rs[y],rs[x],mid+1, R,pos);}intQueryintYintXintLintRintLim) {if(L==R)returnsumv[y]-sumv[x]?lim* (S[y]-s[x])/(Sumv[y]-sumv[x]):0; intMid=l+r>>1; if(Lim<=sumv[ls[y]]-sumv[ls[x]])returnquery (Ls[y],ls[x],l,mid,lim); returnQuery (rs[y],rs[x],mid+1, R,lim-sumv[ls[y]]+sumv[ls[x]]) +s[ls[y]]-s[ls[x]];}intMain () {n=read (); m=read (); Rep (1, N) {Addedge (read (), i); Sv[i]=read (); lv[i]=read (); } DFS (RT); Rep (1, n) Build (root[i],root[i-1],1, M,sv[pos[i]]); Rep (1, N) Ans=max (ans, (LL) lv[i]*query (root[r[i]],root[l[i]-1],1, m,m)); printf ("%lld\n", ans); return 0;}
View Code
BZOJ2809: [apio2012]dispatching