[Apio2012]dispatching

Source: Internet
Author: User

[Apio2012]dispatching Topic

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 each ninja I 's superior Bi, salary Ci, leadership L i, and pay 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≤Bi < I Ninja's superior number

1≤ C i≤m the salary of the ninja;

1 ≤Li≤1,000,000,000 Ninja's leadership level.

INPUT

Reads data from standard input.

The first line contains two integers n and m, where n represents the number of ninjas, andm 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 whole Bi, C i, L i , respectively, representing the superior , salary and leadership of the Ninja of the first . Master satisfies B i = 0, and each ninja's boss's number must be smaller than its own number

OUTPUT

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

Sampleinput

5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1

OUTPUT

6

Problem Solving Report

The surface is sticky, poisonous qwq.

We consider maintaining the weights of each node on the tree and making the selected points the most, so we consider maintaining a large heap on each point, constantly $pop$ heap top know the whole heap of weights and less than the limit can be

Because of the need to merge the heap of subtrees, it becomes a left-leaning tree naked topic qwq

1 /**************************************************************2 problem:28093 User:mafia4 language:c++5 result:accepted6 time:1444 Ms7 memory:9936 KB8 ****************************************************************/9  Ten#include <iostream> One#include <cstring> A#include <cstdio> - using namespacestd; - #defineint long Long theInlineintRead () { -intSum0);CharCH (getchar ()); - for(;ch<'0'|| Ch>'9'; ch=GetChar ()); - for(; ch>='0'&&ch<='9'; sum=sum*Ten+ (ch^ -), ch=GetChar ()); +returnsum; - } + #defineGet_dis (x) (x?x->dis:-1) A #defineGet_sum (x) (x?x->sum:0) at #defineGet_size (x) (x?x->size:0) - structnode{ -Node *lch,*rch; - intkey,sum,dis,size; -Nodeintx=0): LCH (null), RCH (null), key (x), sum (x), Dis (0), Size (1){} -Inlinevoidpushup () { inif(Get_dis ( This->lch) <get_dis ( This-rch)) -Swap This->lch, This-rch); to This->dis=get_dis ( This->rch) +1; +  This->size=get_size ( This->lch) +get_size ( This->rch) +1; - This->sum=get_sum ( This->lch) +get_sum ( This->rch) + This-key; the     } *}*heap[100005]; $Inline node* Merge (node *&x,node *&y) {Panax Notoginsengif(!x)returnYif(!y)returnx; -if(x->key<y->key) Swap (x, y); theX->rch=merge (x->rch,y); +X->pushup (); Areturnx; the } +InlinevoidInsert (node *&x,intv) { -Node *tmp (Newnode (v)); $x=merge (x,tmp); $ } -Inline node* Pop (node *&x) { -if(!x)returnNULL; the returnMerge (x->lch,x->rch); - }Wuyi structedge{ theinte; -Edge *N; Wu}*pre[100005]; -InlinevoidInsertintSinte) { AboutEdge *tmp (Newedge); $Tmp->e=e;tmp->n=pre[s];p re[s]=tmp; - } - intN,m,root; - Long Longans,l[100005]; AInlinevoidDfsintu) { + for(Edge *i=pre[u];i;i=i->N) { theintE (i->e);d FS (e); -heap[u]=merge (Heap[u],heap[e]); $     } the while(heap[u]&&heap[u]->sum>m) theheap[u]=pop (heap[u]); theAns=max (Ans,1ll*get_size (Heap[u]) *L[u]); the } - signed Main () { in //freopen ("dispatching.in", "R", stdin); Freopen ("Dispatching.out", "w", stdout); theN=read (), m=read (); the for(intI=1; i<=n;++i) { About intX (Read ( )), Y (read ()), Z (read ()); theif(!x) root=i; theElseInsert (x,i); theInsert (heap[i],y); l[i]=Z; +     } -DFS (Root);p rintf ("%lld", ans); the}
View Code

[Apio2012]dispatching

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.