码迷,mamicode.com
首页 > Windows程序 > 详细

[APIO2012]派遣

时间:2020-01-27 12:19:26      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:define   typename   while   swa   putc   ring   cst   ios   dig   

[APIO2012]派遣

枚举所有忍者在哪棵子树内, 答案即为本子树内最多派遣的忍者数乘上子树根在原树中祖先最强的领导力, dfs用可并堆合并两棵子树即可, 这道题用不着用并查集维护连通性

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;

template <typename T>
void read(T &x) {
    x = 0; bool f = 0;
    char c = getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
    for (;isdigit(c);c=getchar()) x=x*10+(c^48);
    if (f) x=-x;
}

template <typename T>
void write(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) write(x / 10);
    putchar('0' + x % 10);
}

const int N = 105000;
ll n, m;

inline ll Mx(ll x, ll y) {return x > y ? x : y;}
ll c[N], L[N];
int h[N], to[N], ne[N];
int tot;
inline void add(int x, int y) {
    ne[++tot] = h[x], to[tot] = y, h[x] = tot;
}

int T[N], val[N], f[N], dis[N];
int son[N][2];

#define rs son[x][1]
#define ls son[x][0]
int merge(int x, int y) {
    if (!x || !y) return x | y;
    if (c[x] < c[y]) swap(x, y);
    rs = merge(rs, y);
    if (dis[ls] < dis[rs]) swap(ls, rs);
    dis[x] = dis[rs] + 1;
    return x;
}

ll sum[N], cnt[N], ans;
void dfs(int x) {
    T[x] = x; sum[x] = c[x], cnt[x] = 1;
    for (int i = h[x]; i; i = ne[i]) {
        int y = to[i]; L[y] = Mx(L[x], L[y]);
        dfs(y); T[x] = merge(T[x], T[y]);
        sum[x] += sum[y], cnt[x] += cnt[y];
    }
    while (sum[x] > m) {
        sum[x] -= c[T[x]]; cnt[x]--;
        T[x] = merge(son[T[x]][0], son[T[x]][1]);
    }
    ans = Mx(ans, cnt[x] * L[x]);
}

int main() {
    read(n), read(m);
    for (int i = 1;i <= n; i++) {
        int b; read(b); add(b, i);
        read(c[i]), read(L[i]);
    } dfs(1);
    cout << ans << endl;
    return 0;
}

[APIO2012]派遣

标签:define   typename   while   swa   putc   ring   cst   ios   dig   

原文地址:https://www.cnblogs.com/Hs-black/p/12235634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!