BZOJ2809 [apio2012]dispatching

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, 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 a ninja; 1≤. L i≤1,000,000,000Ninja'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 Input
5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1
Sample Output6

HINT

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. Since 2 ninjas were dispatched and the manager's leadership was 3,

User satisfaction is 2, is the maximum number of user satisfaction can be obtained.

Positive solution: Left-leaning tree

Problem Solving Report:

Just the first thing about the holiday is to learn the left side of the tree ... It's not even open yet .

The problem is to find a node, and in this node as the root node of the subtree to select as many points (the total weight does not exceed the given weight), so that the selected number of points * This node "leadership value" as large as possible.

Looked at the far-off, found that the writing is too simple, so good I decided to write a little more.

First, this topic needs to find a data structure to quickly merge the heap. (As for why wait for the said)

Consider doing it from the bottom up, processing the optimal situation with the current node as the point of choice each time. Obviously the leader value of the node is known, then our task is to select as many nodes in the subtree as possible, so that their weights and values do not exceed the given value. We can maintain a large root heap, Tree[i] represents the root node that is now Dagen when processing to I. We can merge the heap up every time. In addition, if the current weights and values are found to be exceeded, it is clear that the maximum value of the Dagen (that is, the heap top) is deleted, and then the left and right subtrees are merged.

The idea is quite simple, the key is how to merge the heap.

For the consolidation of the heap, the paired heap, Fibonacci heap can be, but the left side of the tree relative difficulty is small, I read the paper learned.

The left side tree is different from the balance tree, the left side tree is the better, so the more unbalanced the better.

How to realize the specific left-leaning tree does not say, the paper inside there.

1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - #ifdef WIN32 - #defineOT "%i64d" the #else - #defineOT "%lld" - #endif - using namespacestd; +typedefLong LongLL; - Const intMAXN =100011; + intN; A LL m; at intFATHER[MAXN],SIZE[MAXN],ABLE[MAXN],D[MAXN],TREE[MAXN]; - LL SUM[MAXN],VAL[MAXN]; - LL ans; - intL[MAXN],R[MAXN];//left and right sub-tree nodes - //Tree[i] Indicates that the weight of the root node with I is a large root heap - //the D array is the distance definition array of the left-leaning tree in  -InlineintGetint () to { +        intw=0, q=0; -        CharC=GetChar (); the         while((c<'0'|| C>'9') && c!='-') c=GetChar (); *        if(c=='-') q=1, c=GetChar (); $         while(c>='0'&& c<='9') w=w*Ten+c-'0', c=GetChar ();Panax Notoginseng        returnQ? -w:w; - } the  +inline ll Max (ll X,ll y) {if(x<y)returnYreturnx;} A  theInlineintMergeintXinty) { +     if(!x)returny; -     if(!y)returnx; $     if(Val[x]<val[y]) swap (x, y);//ensure that the combined root node is a weighted value each time (Dagen) $r[x]=merge (r[x],y); -     if(d[l[x]]<d[r[x]) swap (l[x],r[x]); -d[x]=d[r[x]]+1; thesize[x]=size[l[x]]+size[r[x]]+1; -sum[x]=sum[l[x]]+sum[r[x]]+Val[x];Wuyi     returnx; the } -  WuInlinevoidsolve () { -N=getint (); m=getint (); About      for(intI=1; i<=n;i++) Tree[i]=i,father[i]=getint (), Val[i]=getint (), Able[i]=getint (), sum[i]=val[i],size[i]=1; $d[0]=-1; -      for(inti=n;i>=1; i--) { -      while(sum[tree[i]]>m) Tree[i]=merge (L[tree[i]],r[tree[i]);//if it is out of range, delete the top of the heap (i.e. the largest weight) and merge the left and right subtree -Ans=max (ans, (LL) size[tree[i]]*able[i]); A     if(i!=1) Tree[father[i]]=merge (Tree[i],tree[father[i]);//Merge this node and its father. +     } the printf (Ot,ans); - } $  the intMain () the { the solve (); the   return 0; -}

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