[Apio2012]dispatching

Source: Internet
Author: User

[Apio2012]dispatching time limit: 1 Sec memory limit: MB
The title is described in a ninja gang, where some ninjas are selected to be dispatched to the customer and then 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 the ninja;

1 ≤Li≤1,000,000,000 Ninja'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 input5 40 3 31 3 52 2 21 2 42 3 1Sample output6Tips

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. Because 2 ninjas were dispatched and the manager's leadership was 3, the user's satisfaction was 2, which was the maximum value that could be obtained for user satisfaction.

Exercises

This is a left-leaning tree of the naked problem, can be used deep search, can also be used to search, deep search simple, wide search intuitive, small series used is a wide search.

Establish a left-leaning tree. (with Dagen, because it is only related to the number of ninjas, so the larger the salary is removed, the better)

From the leaves to borrow points up wide search, constantly merging nodes, and then constantly delete the top of the heap until the total wage is less than M.

Then the wide search team when the special sentence, one side of the possibility of ignoring the leaf node.

Here is the AC code:

#include <iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<algorithm>#include<queue>#include<stack>#include<vector>#include<ctime>using namespacestd;intn,m;intfather[100005],cnt[100005];Long Longsum[100005],num[100005],skill[100005];queue<int>Mem;structnode{Long Longkey; intDist; Node*l,*R; intLdist () {returnL?l->dist:0;} intRdist () {returnR?r->dist:0;}} man[100005];node*pos=man,*root[100005];intRead () {intans=0, f=1; CharI=GetChar ();  while(i<'0'|| I>'9'){if(i=='-') f=-1; i=GetChar ();}  while(i>='0'&&i<='9') {ans=ans*Ten+i-'0'; i=GetChar ();} returnans*F;} Node* Merge (node* a,node*b) {    if(!a| |! breturnA?a:b; if(a->key<b->key) Swap (A, b); A->r=merge (a->r,b); if(A->ldist () <a->rdist ()) Swap (a->l,a->R); A->dist=a->rdist () +1; returnA;}voidDelet (intx) {Num[x]--; SUM[X]-=root[x]->key; ROOT[X]=merge (root[x]->l,root[x]->r);}intMain () {inti,j; Long Longans=0; N=read (); M=read ();  for(i=1; i<=n; i++) {Father[i]=read (); Cnt[father[i]]++; scanf ("%lld",&Sum[i]); POS++; POS->key=Sum[i]; POS->l=pos->r=NULL; Num[i]=1; Root[i]=POS; scanf ("%lld",&Skill[i]); }     for(i=1; i<=n; i++)    if(!Cnt[i])        {Mem.push (i); Ans=Max (ans,skill[i]); }     while(!Mem.empty ()) {        intx=Mem.front ();        Mem.pop (); intFa=Father[x]; ROOT[FA]=merge (Root[fa],root[x]); CNT[FA]--; SUM[FA]+=Sum[x]; NUM[FA]+=Num[x]; if(!CNT[FA]) {             while(sum[fa]>m) delet (FA); Ans=max (ans,skill[fa]*NUM[FA]); if(Father[fa]) Mem.push (FA); }} cout<<ans<<Endl; return 0;}

[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.