For each node, select as many nodes as possible in its subtree, and the weights and values of the nodes are less than a fixed value.
Build a large root heap, each node merges from the Son node, and pops the maximum value up to and meets the requirements.
1 /**************************************************************2 problem:28093 user:idy0024 language:c++5 result:accepted6 time:1224 Ms7 memory:6664 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <algorithm> A #defineMax (a) (a) > (b)? ( A):(B)) - #defineN 100010 - using namespacestd; the -typedefLong Longdnt; - - #defineSzof (ND) (ND)? ( nd)->sz:0) + structNode { - intV, SZ; + dnt s; ANode *ls, *rs; atInlinevoidUpdate () { -s = v + (ls?ls->s:0) + (Rs?rs->s:0); -SZ =1+ szof (LS) +Szof (RS); - if(Szof (LS) <Szof (RS)) swap (LS,RS); - } -}pool[n], *tail=pool, *Root[n]; in - intN, M; to intHead[n], dest[n], next[n], Etot; + dnt lead[n], cost[n]; - intQu[n], BG, ed, master; the * voidAdde (intUintv) { $etot++;Panax NotoginsengDest[etot] =v; -Next[etot] =Head[u]; theHead[u] =Etot; + } ANode *newnode (intv) { theNode *nd = + +tail; +Nd->s = Nd->v =v; -Nd->ls = Nd->rs =0; $Nd->sz =1; $ returnnd; - } -Node *smerge (node *na, node *nb) { the if(!na &&!NB)return 0; - if(!na)returnnb;Wuyi if(!NB)returnna; the if(Na->v > nb->v) { -Na->rs = Smerge (na->RS, NB); WuNa->update (); - returnna; About}Else { $Nb->rs = Smerge (nb->rs, NA); -Nb->update (); - returnnb; - } A } + intMain () { thescanf"%d%d", &n, &m); - for(intI=1, p; i<=n; i++ ) { $scanf"%d%lld%lld", &p, Cost+i, lead+i); the if(P) Adde (P, i); the Elsemaster =i; the } theQu[bg=ed=1] =Master; - while(bg<=ed) { in intu=qu[bg++]; the for(intT=head[u]; T t=Next[t]) { the intv=Dest[t]; AboutQu[++ed] =v; the } the } thednt ans =0; + for(inti=ed; i>=1; i-- ) { - intu=Qu[i]; theRoot[u] =NewNode (Cost[u]);Bayi for(intT=head[u]; T t=Next[t]) { the intv=Dest[t]; theRoot[u] =Smerge (Root[u], root[v]); - } - while(Root[u]->s >m) theRoot[u] = Smerge (Root[u]->ls, root[u]->rs); theans = max (ans, lead[u]*root[u]->sz); the } theprintf"%lld\n", ans); -}
View Code
Bzoj 2809 can maintain subtree information with heap