BZOJ2809 [apio2012]dispatching

Source: Internet
Author: User

2809: [apio2012]dispatching time limit:10 Sec Memory limit:128 MB
submit:3807 solved:1977
[Submit] [Status] [Discuss] DescriptionIn a ninja gang, some ninjas are chosen to send to the customer, and then according to their workGet compensation.In this gang, a ninja is calledMaster。 Apart fromMasterOutside, each ninjaAll have and have only one superior. To keep the Ninja's leadership at the same time, all relevant to their workInstructions are always sent by the superior to his direct subordinates, 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, if the manager is not Your goal is to maximize customer satisfaction within your budget. This defines the customer satisfaction for the dispatched Write a program, given every ninja I superior B i , salary ci Span style= "Color:black", leadership l i Ninja's salary budget  m


1 ≤ N ≤100,000 number of ninjas; 1 ≤ M ≤1,000,000,000 Salary total budget;  0 ≤ B i < i Ninja's superior number; 1 ≤ C i≤m Ninja's salary; 1 ≤ L i≤1,000,000,000 Ninja's leadership level.   Inputreads data from standard input.  The first line contains two integers n and m, where n represents the number of ninjas, and m represents the total pre-calculation of the salary .  Next N  i   line contains three whole b Span style= "Font-size:medium" > i, C I, L i Represents the i master meet b i = 0 and each ninja's boss's number must be less than his own number B i < i


Outputoutputs 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.

Heuristic merging of Treap

The answer to each subtree U is l[u]*k (k is a small node in the U subtree and its weight is exactly less than or equal to m)

And then use Treap to merge each subtree into a subtree to maintain the subtrees of treap on Treap, you can find the K of a subtree in O (logn) time.

The complexity of heuristic merging is also O (NLOGN)

Code:

#include <cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#defineREP (i,x,y) for (int i=x;i<y;++i)#definefor (i,x,y) for (int i=x;i<=y;++i)using namespacestd;Const intN = 1e5+5;intN;Long Longm;intF[n],c[n],l[n];inthead[n],to[n<<1],nxt[n<<1],cnt;intRT;voidAddintUintv) {nxt[++cnt]=head[u];head[u]=cnt;to[cnt]=v; nxt[++cnt]=head[v];head[v]=cnt;to[cnt]=u;}structnode{Node*ls,*rs; intSz,rnd,key; Long LongW; voidupd () {W=ls->w+rs->w+key; SZ=ls->sz+rs->sz+1; }};node Node[n* A],*NULL,*Cur;node*Rt[n];structtreap{Inline Node* NewNode (intx) {        ++cur; Cur->ls=cur->rs=NULL; Cur->sz=1; Cur->w=cur->key=x; Cur->rnd=rand (); returncur; }    voidSplit (Node*p,intk,node*&l,node*&R) {        if(!k) {R=p; L=NULL; return; }        if(p->ls->sz+1==k) {L=p;r=p->rs; P->rs=NULL; P-upd (); return; }Else if(p->ls->sz>=k) {R=p; Split (P->ls,k,l,p->ls); P-upd (); }Else{L=p; Split (P->rs,k-1-p->ls->sz,p->rs,r); P-upd (); return; }} node* Merge (node* a,node*b) {        if(a==NULL)returnb; if(b==NULL)returnA; if(a->rnd<b->Rnd) {a->rs=merge (a->rs,b); A-upd (); returnA; }Else{b->ls=merge (a,b->ls); b-upd (); returnb; }    }    intRK (Node*p,intx) {        if(p==NULL)return 0; if(X<=p->key)returnRK (p->ls,x); Else         return 1+p->ls->sz+rk (p->rs,x); }    voidIns (Node*&p,intx) {Node*l,*R;        Split (P,rk (p,x), l,r); P=Merge (merge (L,newnode (x)), R); }    voidDFS (node*&a,node*b) {        if(b==NULL)return; Ins (A, B-key); DFS (A, B-ls); DFS (A, B-RS); }    intQuery (Node*p,Long Longm) {        if(p==NULL)return 0; if(p->ls->w+p->key>m)returnQuery (p->ls,m); Else returnp->ls->sz+1+query (p->rs,m-p->ls->w-p->key); }}t;voidMerge (node*&a,node*&b) {    if(a->sz<b->sz) Swap (A, b); T.dfs (A, b);}Long Longans;voidDfsintUintfat)    {t.ins (rt[u],c[u]);  for(intI=head[u];i;i=Nxt[i]) {        intv=To[i]; if(V==FAT)Continue;        DFS (V,U);    Merge (Rt[u],rt[v]); } ans=max (Ans,1ll*t.query (rt[u],m) *l[u]);}intMain () {NULL=cur=NODE; NULL->ls=NULL->rs=NULL; scanf ("%d%lld",&n,&m); For (I,1, N) rt[i]=NULL; For (I,1, N) scanf ("%d%d%d", f+i,c+i,l+i); For (I,1, N)if(F[i]) Add (I,f[i]); Elsert=i; DFS (RT,-1); cout<<ans<<Endl; 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.