Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=2809
Save the code that can be stacked with the template.
#include <cstdio> #include <cstring> #include <algorithm>const int maxn = 100005, Maxm = 1000000005;int n , M, FA[MAXN], SALARY[MAXN], Lead[maxn];int KEY[MAXN], LEFT[MAXN], RIGHT[MAXN], NPL[MAXN], CNT, Siz[maxn];int HEAD[MAXN], TO[MAXN], NEXT[MAXN], Lb;long long s[maxn], ans;inline void ist (int aa, int ss) {to[lb] = ss;next[lb] = Head[aa];head[aa] = lb;++lb;} int merge (int A, int B) {if (! A) {return B;} if (! B) {return A;} if (Key[b] > Key[a]) {Std::swap (A, B);} Right[a] = merge (Right[a], B), if (Npl[right[a]] > Npl[left[a]]) {Std::swap (Left[a], right[a]);} Npl[a] = Npl[right[a]] + 1;s[a] = S[left[a]] + s[right[a]] + key[a];siz[a] = Siz[left[a] [+ siz[right[a]] + 1;return A;} int dfs (int r) {int rt = ++cnt, tr = -666;key[rt] = Salary[r];s[rt] = Salary[r];siz[rt] = 1;for (int j = head[r]; J! =-1; j = Next[j]) {tr = DFS (to[j]); RT = Merge (RT, TR);} while (S[rt] > m) {rt = Merge (Left[rt], right[rt]);} Ans = Std::max (ans, (long Long) lead[r] * (Long Long) siz[rt]); return RT;} INT Main (void) {//freopen ("In.txt", "R", stdin), memset (head,-1, sizeof head), memset (Next,-1, sizeof next); Npl[0] = -1;sca NF ("%d%d", &n, &m); for (int i = 1; I <= n; ++i) {scanf ("%d%d%d", fa + I, salary + I, leads + i); ist (fa[i], i);} DFS (1);p rintf ("%lld\n", ans); return 0;}
bzoj2806 [apio2012]dispatching "can be stacked"