[Luogu P1552] [APIO2012] Dispatch

Source: Internet
Author: User

[Luogu P1552] [APIO2012] DispatchTopic Background

In a ninja gang, some ninjas are selected to be dispatched to the customer and rewarded for their work.

Title 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/output format

Input format:

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 format:

Outputs a number that represents the maximum value of customer satisfaction in the budget.

input/Output sampleInput Sample # #:
5 40 3 31 3 52 2 21 2 42 3 1
Sample # # of output:
6
Description

The number of 1≤n≤100,000 ninjas;

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.

Think for a long time (in fact, the brain is broken).

My initial idea was that we could fix a point x, think of it as a manager, and then those who were dispatched were looking for it from a subtree rooted in it.

Then Ans=max (Ans,lead[x]*calc (x)). Obviously, that's the right thing to do. Because managers need to be able to reach all the contributors, and managers are selected as the root node of the subtree.

This is convenient for us calc (in fact, the use of a little bit of motion rules and greedy thinking).

So, what about calc? That is, we are going to find the most point cost and not more than m in several subtrees trees of x (including ourselves).

What did you think of? The left-leaning tree seems to be possible.

That is, for each point x, there are several well-disposed left-leaning trees, and then we can combine them with log time.

However, we have to remove some points to satisfy the conditions. I chose to use Dagen, so the removal is convenient.

But what is the specific complexity of this operation? Since the deleted points are not re-added, each point is deleted at most, with a complexity of NLOGN.

Combined with the complexity of the above series of operations Nlogn, the total complexity remains nlogn.

Code

1%:p ragma GCC optimize (2)2#include <bits/stdc++.h>3 #defineLL Long Long4 using namespacestd;5 Const intn=100005;6Vector <int>Son[n];7 intN,m,ro[n]; LL S[n],c[n],f[n],ans;8 structLFT {intL,r,d,w;voidCL () {l=r=d=0;}} A[n];9InlineintRead () {Ten     intx=0;CharCh=GetChar (); One      while(ch<'0'|| Ch>'9') ch=GetChar (); A      while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); -     returnx; - } the intMergeintXinty) { -     if(!x| |! YreturnX+y;if(a[x].w<a[y].w) Swap (x, y); -A[x].r=merge (A[x].r,y);intL=a[x].l,r=A[X].R; -     if(a[l].d<a[r].d) Swap (A[X].L,A[X].R); +A[x].d=a[a[x].r].d+1; -     returnx; + } A intMain () { atN=read (), M=read (), ans=0, a[0].d=-1; -      for(intI=1; i<=n; i++) { -A[i].cl (), ro[i]=I,son[read ()].push_back (i); -S[i]=a[i].w=read (), c[i]=1, f[i]=read (); -     } -      for(intI=n,siz; i>=1; i--) { insiz=son[i].size (); -          for(intk=0, J; k<siz; k++) { toj=son[i][k],ro[i]=merge (Ro[i],ro[j]); +s[i]+=s[j],c[i]+=C[j]; -         } the          for(; s[i]>m;) { *s[i]-=a[ro[i]].w,c[i]--; $ro[i]=merge (A[RO[I]].L,A[RO[I]].R);Panax Notoginseng         } -Ans=max (ans,f[i]*c[i]); the     } +printf"%lld\n", ans); A     return 0; the}
View Code

[Luogu P1552] [APIO2012] Dispatch

Related Article

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.