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 that gives the top Bi of each Ninja I, salary ci, leadership l I, and the salary budget M paid to the ninja, outputting the maximum customer satisfaction when the budget meets the above requirements.
Solution
Can and Heap
Enumeration manager, the Ninja that can be removed in its subtree, if the salary exceeds the budget, the largest element pops up
Keep merging up
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#defineMAXN 100005typedefLong LongLL;using namespacestd;intn,m,b[maxn],c[maxn],l[maxn],tot=0, ROOT[MAXN]; LL ans=0;structnode{intLch,rch,w,d,sz,sum;} HEAP[MAXN];voidUpdateintx) {HEAP[X].D=heap[heap[x].rch].d+1; Heap[x].sz=heap[heap[x].lch].sz+heap[heap[x].rch].sz+1; Heap[x].sum=heap[heap[x].lch].sum+heap[heap[x].rch].sum+HEAP[X].W;}intMergeintXinty) { if(!x| |! Yreturnx+y; if(heap[x].w<heap[y].w) Swap (x, y); Heap[x].rch=merge (Heap[x].rch,y); if(heap[heap[x].lch].d<heap[heap[x].rch].d) Swap (heap[x].lch,heap[x].rch); Update (x); returnx;}intInsertintW) {heap[++tot].d=0, heap[tot].sz=1; Heap[tot].lch=heap[tot].rch=0; Heap[tot].sum=heap[tot].w=W; returntot;}voidPopint&x) {x=merge (Heap[x].lch,heap[x].rch);}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) {scanf ("%d%d%d",&b[i],&c[i],&L[i]); Root[i]=Insert (c[i]); while(heap[root[i]].sum>m) pop (root[i]); } for(intI=n;i>0; i--) {ans=max (ans,1ll*heap[root[i]].sz*L[i]); Root[b[i]]=merge (Root[i],root[b[i]]); while(heap[root[b[i]]].sum>m) pop (Root[b[i]]); } printf ("%lld", ans); return 0;}
[Bzoj 2809] [Apio2012]dispatching (left-leaning tree)