[APIO2012] Dispatch

Source: Internet
Author: User

P1552 [APIO2012] dispatch https://www.luogu.org/problemnew/show/P1552 topic background

In a ninja gang, some ninjas are selected to be dispatched to the customer and rewarded for their work.

Title Description

In this gang, there is a ninja called \ (master\) . In addition to the \ (master\) , each ninja has and has only one ancestor. 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 want 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, you don't need to pay a manager's salary if the manager is not being taken.
Your goal is to maximize customer satisfaction within your 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\) ancestor \ (b_i\) , salary \ (c_i\) , leadership \ (l_i\) , and the total salary paid to ninjas for the total budget \ (m\) , which outputs the maximum customer satisfaction value when the above requirements are met in the budget.

Input format:
The first line contains two integers \ (n\) and \ (m\), where N represents the number of ninjas,\ (m\) represents the total budget for the salary.
Next \ (n\) line describes the Ninja's superiors, salary, and leadership. The first \ (i\) line contains three integers \ (b_i\),\ (c_i\), and\ (l_i\) respectively represents the ancestor, salary, and leadership of the first ( i\) Ninja. \ (master\) satisfies \ (b_i=0\), and each ninja's boss's number must be smaller than its own number \ (b_i<i\).

Output format:

Outputs a number that represents the maximum value of customer satisfaction in the budget.

Input Sample:

5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1

Sample output:

6

Description

The number of 1≤n≤100,000 ninjas;
1≤m≤1,000,000,000 salary total budget;
0≤bi < I ninja's superior number;
1≤ci≤m Ninja's salary;
1≤li≤1,000,000,000 Ninja's leadership level.
For 30% of data, n≤3000.

Enumerating each point as a manager, the topic is evaluated as a weighted value in the subtree and a set of points less than \ (m\) , maximizing the number of points in that point set.
Set the number of points to \ (num_i\), then there is the final answer \ (Ans=max (ans,l_i*num_i) \).
Consider building a heap on each node, and for each non-leaf node, merge the node with each son node (left-leaning tree), maintaining the node count in the heap and the point weights and the nodes in the heap .

细节:sum[]和Ans需要开long long
#define LL Long long#define RG register#include<cstdio> #include <iostream>using namespace Std;const int n= 100005;inline int Read () {RG int x=0,w=1;    RG Char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') W=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x*w;} int n,m,cnt; LL Ans;int B[n],c[n],l[n],last[n],num[n],root[n],ls[n],rs[n],dist[n]; LL sum[n];struct edge{int to,next;} e[n];inline void Insert (int u,int v) {e[++cnt]= (edge) {v,last[u]};last[u]=cnt;} int find (int x) {return x==root[x]?x:root[x]=find (root[x]);} and search for the root, path compression int Merge (int a,int b) {if (!a| |!    b) return a+b;    if (C[a]<c[b]) swap (A, b);    Rs[a]=merge (RS[A],B);    if (Dist[rs[a]]>dist[ls[a]]) swap (rs[a],ls[a]);    dist[a]=dist[rs[a]]+1; return A;}    void Dfs (int now)//down DFS from the root node, merging {num[now]=1;//build heap sum[now]=c[now] when backtracking;        for (RG int i=last[now];i;i=e[i].next) {int v=e[i].to;        DFS (v); int Fv=find (v), Fn=find (now);//Find the root of the heap where the node is located [Now and V are not necessarily the root of the heap] root[now]=merge (FN,FV);//merge the subordinate SUM[NOW]+=SUM[V];        NUM[NOW]+=NUM[V];            while (sum[now]>m)//point weights and greater than M require pop {num[now]--;            Sum[now]-=c[root[now]];            int Ll=ls[root[now]],rr=rs[root[now]]; Root[now]=merge (LL,RR);//delete heap top root[ll]=ll==root[now]?ll:root[ll];//to prevent the death of the root when the cycle, the new root of the left side of the tree root to set itself ROOT[RR        ]=RR==ROOT[NOW]?RR:ROOT[RR]; }} Ans=max (Ans,1ll*l[now]*num[now]);}    int main () {n=read ();    M=read ();        for (RG int i=1;i<=n;i++) {root[i]=i;        B[i]=read ();        C[i]=read ();        L[i]=read ();    if (B[i]) insert (b[i],i);    } dfs (1);    printf ("%lld\n", Ans); return 0;}

[APIO2012] Dispatch

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.