https://www.lydsy.com/JudgeOnline/problem.php?id=2809
The board question WA a bit because output ans has no LLD
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7 using namespacestd;8 Const intmaxn=100100;9 intn,m;Ten intch[maxn][2]={},siz[maxn]={},sum[maxn]={},cnt[maxn]={},rt[maxn]={}; One intfa[maxn]={},val[maxn]={},l[maxn]={}; A inty[maxn],nex[maxn]={},head[maxn]={},tot=0; - Long Longans=0; - voidInitintXintYi) { they[++tot]=yi;nex[tot]=head[x];head[x]=tot; - } - voidUpdata (intx) { -siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1; +sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+Val[x]; - } + intMergeintXinty) { A if(x==0)returnYif(y==0)returnx; at if(val[x]<Val[y]) swap (x, y); -ch[x][1]=merge (ch[x][1],y); - if(cnt[ch[x][1]]>cnt[ch[x][0]]) -Swap (ch[x][1],ch[x][0]); -cnt[x]=cnt[ch[x][1]]+1; - updata (x); in returnx; - } to voidDfsintx) { + for(intI=head[x];i;i=Nex[i]) { - DFS (Y[i]); thert[x]=merge (Rt[x],rt[y[i]]); * while(sum[rt[x]]>m) {Rt[x]=merge (ch[rt[x]][0],ch[rt[x]][1]);} $}ans=max (ans, (Long Long) l[x]* (Long Long) siz[rt[x]);Panax Notoginseng } - intMain () { thescanf"%d%d",&n,&m); + for(intI=1; i<=n;i++){ Ascanf"%d%d%d",&fa[i],&val[i],&l[i]); thesum[i]=val[i];rt[i]=i;siz[i]=1; + if(Fa[i]) init (fa[i],i); - } $Dfs1); $printf"%lld\n", ans); - return 0; -}
View Code
2809: [apio2012]dispatching can be stacked with left-leaning tree