Test Instructions:Link
Method:Can and Heap
parsing:Water problem, but notice the process burst Int. The method is to find the root node of the tree, then sweep, each subtree as a heap, and then merge between, if the heap sum and more than M, then remove the largest, continue to add, this is obviously ah. Then each time a point is processed, the answer is updated with the solution in the heap. than the first two water
Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 100100Using namespace Std;typedef long long ll;int n,m,all_root,cnt;ll ans;int head[N],h[N],fa[N],ch[N][2],root[n];ll sum[n],val[n],lead[n],size[n];struct node{int from,to,next;}edge[n];int find (int x) {return fa[x]==x?fa[x]:fa[x]=find (fa[x]);} void pushup (int x) {sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];size[x]=size[ch[x][0]]+size[ch[x][1]]+1;} void Init () {memset (head,-1,sizeof (head)), cnt=1;for (int i=1;i<=n;i++) fa[i]=i;}void edgeadd (int from,int to) {edge[cnt].to=to,edge[cnt].next=head[from],head[from]=cnt++;}int merge (int x,int y) {if (!x) return y;if (!y) return x;if (Val[x]<val[y]) swap (x, y);Ch[x][1]=merge (ch[x][1],y);if (h[ch[x][1]]>h[ch[x][0]]) swap (ch[x][1],ch[x][0]);h[x]=h[ch[x][1]]+1;return x;}void dfs (int x) {sum[x]=val[x],size[x]=1;for (int i=head[x];i!=-1;i=edge[i].next) {int to=edge[i].to;Dfs (to);Sum[x]+=sum[to];size[x]+=size[to];Fa[x]=merge (Fa[x],fa[to]); }While (sum[x]>m) {sum[x]-=val[fa[x]],size[x]--;Fa[x]=merge (ch[fa[x]][0],ch[fa[x]][1]); }Ans=max (Lead[x]*size[x],ans);}int Main () {H[0]=-1;scanf ("%d%d", &n,&m);init ();for (int i=1;i<=n;i++) {int f,v,l;scanf ("%d%d%d", &f,&v,&l);if (!f) {all_root=i,val[i]=v,lead[i]=l,size[i]=1;continue;}Edgeadd (f,i);val[i]=v,lead[i]=l,size[i]=1; }DFS (all_root);printf ("%lld\n", ans);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 2809 [apio2012]dispatching can be stacked