"The main 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.
Ideas
With Dfs, from bottom to top, each time the heap is merged, if the total salary is greater than the expected value, the heap top (that is, the highest-paid one) pops up, until it is less than the expected value, and each DFS updates the answer.
"Error Point"
It's written in the program! The comparison of the keywords is cost is not sum!!!!!!!!
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <vector>6 using namespacestd;7typedefLong Longll;8 Const intmaxn=100000+ -;9 structnodeTen { One ll Cost,key,sum; A intSize,dis,lson,rson; - }LTREE[MAXN]; - intN,master; thell m,ans=-1; -vector<int>E[MAXN]; - - voidBuildintXintfa,ll salary,ll leading) + { -ltree[x].cost=ltree[x].sum=salary; +ltree[x].key=leading; ALtree[x].size= (x==0)?0:1; atltree[x].lson=ltree[x].rson=0; -Ltree[x].dis= (x==0)?-1:0; - } - - voidPushup (intx) - { in intL=ltree[x].lson,r=Ltree[x].rson; -ltree[x].sum=ltree[x].cost+ltree[l].sum+ltree[r].sum; toLtree[x].size=1+ltree[l].size+ltree[r].size; + } - the intMergeintXinty) * { $ if(x==0)returny;Panax Notoginseng if(y==0)returnx; - if(ltree[x].cost<ltree[y].cost) Swap (x, y); the /*Here The comparison size is between cost, not sum ... Checked for an afternoon before discovering!!!!!! */ +ltree[x].rson=merge (ltree[x].rson,y); A int&l=ltree[x].lson,&r=Ltree[x].rson; the if(ltree[l].dis<Ltree[r].dis) Swap (l,r); + if(r==0) ltree[x].dis=0; - Elseltree[x].dis=ltree[r].dis+1; $ pushup (x); $ returnx; - } - the intDelintRT) - {Wuyi intL=Ltree[rt].lson; the intR=Ltree[rt].rson; -ltree[rt].dis=ltree[rt].lson=ltree[rt].rson=0; Wu returnmerge (l,r); - } About $ - voidInit () - { -scanf"%d",&n); Ascanf"%lld",&m); + for(intI=1; i<=n;i++) the { - intb; $ Long Longc,l; thescanf"%d%lld%lld",&b,&c,&l); the if(b==0) master=i; the E[b].push_back (i); the build (i,b,c,l); - } inBuild0,0,0,0); the } the About intDfsintu) the { the intrt=u; the for(intI=0; I<e[u].size (); i++) + { - intto=E[u][i]; thert=Merge (Rt,dfs (to));Bayi } the while(ltree[rt].sum>m) the { -ltree[rt].sum-=Ltree[rt].cost; -ltree[rt].size--; thert=del (RT); the } theAns=max (ans, (LL) ltree[rt].size*(ll) ltree[u].key); the returnRT; - } the the voidPrint_ans () the {94printf"%lld", ans); the } the the intMain ()98 { About //freopen ("dispatching.in", "R", stdin); - //freopen ("Dispatching.out", "w", stdout);101 init ();102 DFS (master);103 Print_ans ();104 return 0; the}
"Left-leaning tree" bzoj2809-[apio2012]dispatching