[Submit] [Status] [Discuss] 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 Input5 4
0 3 3
1 3 5
2 2 2
1 2 4
2 3 1
Sample Output6
Idea {
Left side of the tree understand, but through the problem can still be found not skilled.
First, it is easy to think of a tree DP approach to merge the solutions upward.
It is thought that the use can be stacked. But is it a Dagen or a little Gan?
Joscheggen ( which I wrote most initially ), should be counted sequentially, until and above m , all other pops
See, this complexity is relatively high. ( But I don't have TLE,WA ...) )
Dagen the words, the elements pop up until the elements and <=m. Relatively easy to implement.
In the process of DFS , in order to prevent conflicts between variables,
Also set an array to represent the heap to which the current point belongs. (Maybe I'll just WA here ....) )
In order to speed over PZ on the cogs, I actually used the read-in optimization ...
}
#include <bits/stdc++.h> #define RG register#define il inline #define N 100010#define LL long longusing namespace std; int v[n],l[n],r[n],d[n],l[n],n,head[n],tot,bl[n],sz[n]; LL sum[n];struct ed{int nxt,to;} E[n];int m; LL ans;void Add (RG int U,rg int v) {e[tot].nxt=head[u];e[tot].to=v;head[u]=tot++;} Il int merge (RG int x,rg int y) {if (!x| |! y) return x+y; if (V[x]<v[y]) swap (x, y); R[x]=merge (R[x],y); if (D[r[x]]>d[l[x]]) swap (r[x],l[x]); D[x]=d[r[x]]+1;return x;} Il int getint () {int W=0;bool q=0; Char C=getchar (); while (c> ' 9 ' | | c< ' 0 ') &&c!= '-') C=getchar (); if (c== '-') C=getchar (), q=1; while (c>= ' 0 ' &&c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); return q?-w:w;} Il void dfs (int u) {bl[u]=u;sum[u]=v[u];sz[u]=1; for (RG int i=head[u];i!=-1;i=e[i].nxt) {RG int v=e[i].to; DFS (v); Sum[u]+=sum[v]; SZ[U]+=SZ[V]; Bl[u]=merge (Bl[u],bl[v]); }while (Sum[u]>m&&sz[u]) {sum[u]-=v[bl[u]]; Bl[u]=merge (R[bl[u]],l[bl[u]); sz[u]--; }ans=max (ans, (LL) sz[u]*l[u]);} int main () {freopen ("dispatching.in", "R", stdin); Freopen ("Dispatching.out", "w", stdout); memset (head,-1,sizeof (head)); N=getint (), M=getint (); for (RG int i=1;i<=n;++i) {int ytk; Ytk=getint (), V[i]=getint (), L[i]=getint (); Add (ytk,i); }dfs (1);p rintf ("%lld", ans); return 0;}
BZOJ2809: [apio2012]dispatching