Background
In a ninja gang, some ninjas are selected to be dispatched to the customer and rewarded for their work.
Description
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, you don't need to pay a manager's salary if the manager is not being taken.
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 that gives the top bi of each ninja I, salary ci, leadership Li, and the total salary of the ninja to pay the total budget m, outputting the maximum customer satisfaction when the budget meets the above requirements.
Input
The first line contains two integers n and M, where n represents the number of ninjas, and M represents the total budget for the salary.
The next n lines describe the Ninja's superiors, their salary, and their leadership. The line I contains three integers bi,ci,li, respectively, representing the superior, salary, and leadership of the Ninja of the first. Master satisfies Bi=0, and each ninja's boss's number must be smaller than its own number bi<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 Output
6
Hint
$1≤n≤100,000 $ ninja number;
$1≤m≤1,000,000,000 $ salary total budget;
$0≤bi < I $ ninja's superior number;
\ (1≤ci≤m\) Ninja's salary;
$1≤li≤1,000,000,000 $ Ninja's leadership level.
For 30% of data, n≤3000.
Solution
Left-biased tree maintenance of the maximum value, weight and, subtree size, and then each second son of the tree merge, and finally pop-up containing the node's left-biased tree as little as possible, so that the left-leaning tree within the weight and not more than M, greedy can, update the answer. Time complexity \ (O (n \log N).
Code
#include <stdio.h>#include <string.h>#include <algorithm>#define MN 100005#define R Register#define LL Long Long#define File (x) freopen (#x ". In", "R", stdin), Freopen (#x ". Out", "w", stdout)#define Endfile fclose (stdin), fclose (stdout)inline intRead () {RintX RBOOLF RCharC for(f=0; (C=getchar ()) <' 0 '|| C>' 9 '; f=c=='-'); for(x=c-' 0 '; (C=getchar ()) >=' 0 '&&c<=' 9 '; X= (x<<3) + (x<<1) +c-' 0 ');returnf?-x:x;}inlinell Max (ll A,ll b) {returnA>B?A:B;}inline intMaxintAintb) {returnA>B?A:B;}inline intMinintAintb) {returnA<B?A:B;}inline voidSwapint&a,int&B) {a^=b,b^=a,a^=b;}intch[mn][2],ht[mn],n,m,to[mn],nt[mn],h[mn],vl[mn],v[mn],sz[mn],rt,en;ll Sum[mn],ans;inline voidInsintUintV) {if(!u)return(void) (RT=V); to[++en]=v,nt[en]=h[u],h[u]=en;}inline intMergeintXintY) {if(!x| |! YreturnX+y;if(Vl[x]<vl[y]) swap (x, y); ch[x][1]=merge (ch[x][1],y);if(ht[ch[x][1]]>ht[ch[x][0]]) Swap (ch[x][0],ch[x][1]); sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1; sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+VL[X]; ht[x]=ht[ch[x][1]]+1;returnx;}inline intDfsintu) {RintTmp=u; for(RintI=h[u]; I I=nt[i]) Tmp=merge (Tmp,dfs (To[i])); while(sum[tmp]>m) Tmp=merge (ch[tmp][0],ch[tmp][1]); Ans=max (ans, (LL) sz[tmp]*v[u]);returnTMP;}intMain () {N=read (), M=read (); ht[0]=-1; for(RintI=1; i<=n; ++i) Ins (read (), i), Sum[i]=vl[i]=read (), V[i]=read (), sz[i]=1; DFS (RT);p rintf ("%lld\n", ans); Endfile;return 0;}
[APIO2012] [BZOJ2809] Dispatch