#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define int long long
int N, s, cur, x[300005], p[300005];
bool took[300005];
vector<int> adj[300005], takers[300005];
namespace ufds {
int lnk[300005], sz[300005], l[300005], v[300005], highest[300005];
void init(int n) {
iota(lnk, lnk + 1 + n, 0);
iota(highest, highest + 1 + n, 0);
fill(sz, sz + 1 + n, 1);
}
int find(int x) {
if (x == lnk[x]) {
return x;
}
return lnk[x] = find(lnk[x]);
}
void unite(int a, int b) {
//~ cout << "MERGE " << a << ' ' << b << '\n';
a = find(a);
b = find(b);
if (a == b) {
return;
}
if (sz[b] > sz[a]) {
swap(a, b);
}
sz[a] += sz[b];
lnk[b] = a;
highest[a] = highest[b];
}
};
void pass(int u) {
if (u && ufds::find(u) != ufds::find(p[u])) {
int l_p = ufds::l[ufds::find(p[u])];
int v_p = ufds::v[ufds::find(p[u])];
int l_u = ufds::l[ufds::find(u)];
int v_u = ufds::v[ufds::find(u)];
//~ cout << "@ " << u << ' ' << l_u << ' ' << v_u << ' ' << l_p << ' ' << v_p << '\n';
if (l_p + v_p >= l_u && v_u >= 0) {
ufds::unite(u, p[u]);
ufds::l[ufds::find(u)] = l_p;
ufds::v[ufds::find(u)] = v_p + v_u;
}
}
}
int dfs(int u, int sf = 0) {
//~ cout << "@ " << u << ' ' << sf << ' ' << ufds::l[ufds::find(u)] << ' ' << sf << '\n';
if (u == ufds::highest[ufds::find(u)] && ufds::l[ufds::find(u)] - sf > cur) {
return 0;
}
int tmp = 0;
if (!took[u] && u == ufds::highest[ufds::find(u)]) {
tmp += ufds::v[ufds::find(u)];
sf += ufds::v[ufds::find(u)];
takers[u].pb(u);
}
for (auto v : adj[u]) {
tmp += dfs(v, sf);
if (takers[v].size() > takers[u].size()) {
swap(takers[u], takers[v]);
}
for (auto w : takers[v]) {
takers[u].pb(w);
}
}
return max(tmp, 0ll);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> s;
ufds::l[0] = 0;
ufds::v[0] = s;
for (int i = 1; i <= N; i++) {
cin >> x[i] >> p[i];
adj[p[i]].pb(i);
ufds::l[i] = max(0ll, -x[i]);
ufds::v[i] = x[i];
}
ufds::init(N);
for (int rep = 0; rep < N; rep++) {
for (int i = 1; i <= N; i++) {
pass(i);
}
}
for (int rep = 0; rep < N; rep++) {
cur += dfs(0);
for (auto i : takers[0]) {
took[i] = 1;
}
//~ cout << "CUR " << cur << '\n';
}
cout << cur - s << '\n';
}
Compilation message
Main.cpp:80:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
80 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2016 ms |
39420 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
4 |
Correct |
40 ms |
27080 KB |
Output is correct |
5 |
Runtime error |
586 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
4 |
Correct |
40 ms |
27080 KB |
Output is correct |
5 |
Runtime error |
586 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
Output is correct |
2 |
Correct |
4 ms |
26972 KB |
Output is correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
4 |
Correct |
40 ms |
27080 KB |
Output is correct |
5 |
Runtime error |
586 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2016 ms |
39420 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |