您的位置:首页 > 其它

【BZOJ2809】【APIO2012】dispatching

2018-03-18 19:45 411 查看
【题目链接】
点击打开链接

【思路要点】
补档博客,无题解。
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN	100005
struct Node {
int father, size;
long long value, sum;
int child[2], depth[2];
};
Node a[MAXN];
vector <int> t[MAXN];
int n, master, b[MAXN], root[MAXN];
long long m, ans, c[MAXN], l[MAXN];
int merge(int x, int y) {
if (x == 0) return y;
if (y == 0) return x;
if (a[x].value < a[y].value) swap(x, y);
a[x].size += a[y].size;
a[x].sum += a[y].sum;
a[x].child[1] = merge(a[x].child[1], y);
a[a[x].child[1]].father = x;
a[x].depth[1] = 1 + max(a[a[x].child[1]].depth[0], a[a[x].child[1]].depth[1]);
if (a[x].depth[1] > a[x].depth[0]) {
swap(a[x].depth[0], a[x].depth[1]);
swap(a[x].child[0], a[x].child[1]);
}
return x;
}
int del(int x) {
int tmp = merge(a[x].child[0], a[x].child[1]);
a[tmp].father = 0;
return tmp;
}
void work(int pos) {
for (unsigned i = 0; i < t[pos].size(); i++) {
work(t[pos][i]);
root[pos] = merge(root[pos], root[t[pos][i]]);
}
while (a[root[pos]].sum > m)
root[pos] = del(root[pos]);
ans = max(ans, l[pos] * a[root[pos]].size);
}
int main() {
scanf("%d%lld", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d%lld%lld", &b[i], &c[i], &l[i]);
a[i].value = c[i];
a[i].sum = c[i];
a[i].size = 1;
root[i] = i;
if (b[i] != 0) t[b[i]].push_back(i);
else master = i;
}
work(master);
printf("%lld\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: