"Bzoj 2809" 2809: [Apio2012]dispatching (left-leaning tree)

Source: Internet
Author: User

2809: [apio2012]dispatching 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 Output 6

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.

Source

Analysis

Enumeration is managed, because it is certainly the smallest of those who choose the subtree, so maintain the maximum heap, if and greater than the limit, the largest pop out, with left-biased tree maintenance merge operation.

So that's nlogn.

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 #defineMAXN 1000108 #defineLL Long Long9 Ten structnode One { A     intX,y,next; -}T[MAXN];intlen=0; -  the intW[MAXN],RT[MAXN],FIRST[MAXN]; - intn,m; -  - intMymin (intXintY) {returnX<y?x:y;} + //int Mymax (int x,int y) {return x>y?x:y;} -ll Mymax (ll X,ll y) {returnX>y?x:y;} +  A voidInsintXinty) at { -t[++len].x=x;t[len].y=y; -t[len].next=first[x];first[x]=Len; - } -  -  in intC[MAXN],LC[MAXN],RC[MAXN],DIS[MAXN],SIZ[MAXN]; - LL H[MAXN]; to intMergeintXinty) + { -     if(x==0|| y==0)returnx+y; the     if(c[x]<C[y]) swap (x, y); *rc[x]=merge (rc[x],y); $     if(dis[lc[x]]<dis[rc[x]) swap (lc[x],rc[x]);Panax Notoginsengh[x]=c[x]+h[lc[x]]+H[rc[x]]; -siz[x]=siz[lc[x]]+siz[rc[x]]+1; thedis[x]=dis[rc[x]]+1; +     returnx; A } the  + intRttintx) - { $     if(x==0)return 0; $     if(X!=rt[x]) rt[x]=RTT (rt[x]); -     returnRt[x]; - } the  -LL ans=0;Wuyi voidFfind (intx) the { -      for(intI=first[x];i;i=t[i].next) Ffind (T[I].Y); Wu     intnw=RTT (x); -      for(intI=first[x];i;i=t[i].next) About     { $         intxx=NW; -nw=Merge (Nw,rtt (T[I].Y)); -Rt[xx]=rt[rtt (T[I].Y)]=NW; -     } A      while(h[nw]>m) +     { the         intxx=NW; -nw=merge (LC[NW],RC[NW]); $rt[lc[xx]]=rt[rc[xx]]=NW; thert[xx]=NW; the     } theLL x=w[x],y=SIZ[NW]; the     //Ans=mymax (ans, (LL) w[x]* (LL) siz[nw]); -Ans=mymax (ans,x*Y); in } the  the intMain () About { thescanf"%d%d",&n,&m); thememset (First,0,sizeof(first)); the     intsum=0;d is[0]=-1; +      for(intI=1; i<=n;i++) -     { the         intx, y, zBayiscanf"%d%d%d",&x,&y,&z); theIns (x,i); c[i]=y;w[i]=Z; thert[i]=i;h[i]=c[i];siz[i]=1; -     } -Ffind (1); theprintf"%lld\n", ans); the     return 0; the}
View Code

2017-01-18 14:50:24

"Bzoj 2809" 2809: [Apio2012]dispatching (left-leaning tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.