"APIO2012" dispatch

"APIO2012" dispatch

Portal
when the budget exceeds the limit, losing a high priority salaries Ninja (leftist tree maintenance it), then take the maximum legal answer answer.
Reference Code:

#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

typedef long long LL;
const int _ = 1e5 + 5;

int tot, head[_], nxt[_], ver[_];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }

int n, m, rt, c[_], l[_], siz[_]; LL sum[_], ans;
int tr[_], dis[_], lc[_], rc[_];

inline int merge(int x, int y) {
    if (!x || !y) return x + y;
    if (c[x] < c[y]) swap(x, y);
    rc[x] = merge(rc[x], y);
    if (dis[lc[x]] <  dis[rc[x]]) swap(lc[x], rc[x]);
    dis[x] = dis[rc[x]] + 1;
    return x;
}

inline void dfs(int u, int f) {
    siz[u] = 1, sum[u] = c[u], tr[u] = u;
    for (rg int i = head[u]; i; i = nxt[i]) {
        int v = ver[i]; if (v == f) continue ;
        dfs(v, u), siz[u] += siz[v], sum[u] += sum[v], tr[u] = merge(tr[u], tr[v]);
    }
    while (sum[u] > m)
        sum[u] -= c[tr[u]], --siz[u], tr[u] = merge(lc[tr[u]], rc[tr[u]]);
    ans = max(ans, 1ll * siz[u] * l[u]);
}

int main() {
    read(n), read(m);
    for (rg int f, i = 1; i <= n; ++i) {
        read(f), read(c[i]), read(l[i]);
        if (!f) rt = i; else Add_edge(f, i);
    }
    dfs(1, 0);
    printf("%lld\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zsbzsb/p/12231666.html