[bzoj2809] [Apio2012] [Dispatching] (Can be stacked)

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.

Solution

This is the strategy.

Solution optimality: When the whole tree is DFS, we actually enumerate each manager, take the best

and solve the problem of optimality, the rest of us to do is to try to use full funding

Solution feasibility: For every subtrees tree, we delete the largest node when its wage value is not enough, and each backtracking needs to update the maximum value of multiple subtrees managed by the current node, which requires the use of a heap to implement

#include <stdio.h> #define MAXN 100010#define DMax (a) (a) > (b)? ( A):(B)) #define MAXBUF 1<<22#define GEC ((s==t&& (t= (s=b) +fread (B,1,maxbuf,stdin), s==t))? 0:*s++) Char B[maxbuf],*s=b,*t=b;template<typename type> inline void Rin (Type &x) {int C=gec (); for (; c<48| | C&GT;57;C=GEC ()); for (x=0;c>47&&c<58;x= (x<<1) + (x<<3) +c-48,c=gec ()); }inline void swap (int &x,int &y) {x^=y,y^=x,x^=y;} int N,m,c[maxn],l[maxn],tot,root[maxn],ls[maxn],rs[maxn],va[maxn];long Long Sum[maxn],size[maxn],ans;struct Pointer {int V; Pointer *next; }*fir[maxn],mem[maxn],*nil=mem;inline void link (int x,int y) {*++nil= (Pointer) {y,fir[x]},fir[x]=nil;} int merge (int x,int y) {if (!) ( x&&y)) return x|y;if (Va[x]<va[y]) swap (x, y), Rs[x]=merge (Rs[x],y), swap (ls[x],rs[x]), return x; inline void pop (int &x) {x=merge (ls[x],rs[x]);} void _dfs (int at) {root[at]=++tot,va[tot]=c[at],size[at]=1,sum[at]=c[at];for (Pointer *iter=fir[at];iter;iter=iter- >next) _dfs (ITER-&GT;V), Sum[at]+=sum[iter->v],size[at]+=size[iter->v],root[at]=merge (Root[at],root[iter->v]); while ( sum[at]>m) Size[at]--, Sum[at]-=va[root[at]],pop (Root[at]); Ans=dmax (Ans,l[at]*size[at]);} int main () {rin (n), Rin (M), for (int i=1,x;i<=n;i++) rin (x), Rin (C[i]), Rin (l[i]), Link (x,i), _dfs (1), printf ("%lld\n", ANS); return 0; }

  

[bzoj2809] [Apio2012] [Dispatching] (Can be stacked)

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.