[Bzoj 2809] [Apio2012]dispatching (left-leaning tree)

Source: Internet
Author: User

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)

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.