标签:define typename while swa putc ring cst ios dig
枚举所有忍者在哪棵子树内, 答案即为本子树内最多派遣的忍者数乘上子树根在原树中祖先最强的领导力, 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;
}
标签:define typename while swa putc ring cst ios dig
原文地址:https://www.cnblogs.com/Hs-black/p/12235634.html