#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 2e5 + 5;
const int inf = 1e9 + 5 + 7 + 9;
vector<int> g[nmax];
int father[nmax], cost[nmax], height[nmax];
vector<int> naive[nmax];
int solved[nmax];
template<class CB> void fill(int a, int f, CB&& cb, vector<int> graph[nmax]) {
cb(a);
for(auto x : graph[a])
if(x != f)
fill(x, a, cb, graph);
return;
}
#define lsb(x) (x & -x)
struct AIB {
vector<int> v;
void init(int n_) { v.assign(n_ + 5, 0); }
int query(int a) {
int sum = 0;
while(a > 0) sum += v[a], a -= lsb(a);
return sum;
}
void update(int p, int x) {
while(p < sz(v)) v[p] += x, p += lsb(p);
}
};
namespace DS {
ll sol;
AIB occupied;
int pin[nmax], pout[nmax], inp;
int p[nmax][18], d[nmax];
int budget[nmax];
void fill(int a, int f) {
//cerr << a << ' ' << f << '\n';
d[a] = d[f] + 1;
p[a][0] = f;
for(int i = 1; i < 18; i++) p[a][i] = p[p[a][i - 1]][i - 1];
pin[a] = inp++;
for(auto x : g[a])
if(x != f)
fill(x, a);
pout[a] = inp;
return;
}
void startProcedure(vector<int> nodes, vector<int> cycles) {
height[0] = -inf;
sol = 0;
inp = 1;
fill(0, 0);
map<int, ll> cyclefree;
for(auto x : cycles) solved[x] = 100, d[x] = -1;
copy(all(cycles), back_inserter(nodes));
map<pii, int> renrm;
for(auto x : nodes) renrm[make_pair(height[x], d[x])];
int cnt = 0;
for(auto &[a, b] : renrm) b = cnt++;
for(auto x : nodes) height[x] = renrm[make_pair(height[x], d[x])];
for(auto x : nodes) {
if(solved[x] == 100)
cyclefree[height[x]] += cost[x];
}
occupied.init(inp);
//cerr << "wow\n";
ll sum = 0, maxpossible = 0;
for(auto x : nodes) sum += cost[x];
sort(all(nodes), [&](int a, int b) { return height[a] > height[b]; });
for(auto x : nodes) budget[x] = 0;
auto anc = [&](int node) { return occupied.query(pin[node]); };
auto modnode = [&](int node, int p) { occupied.update(pin[node], p), occupied.update(pout[node], -p); };
for(auto x : nodes) {
int specialed = 0;
if(solved[x] == 100) {
specialed = cyclefree[height[x]];
}
else {
int mine = anc(x), upward = x, mybudget = cost[x];
while(mybudget > 0 && mine > 0) {
for(int i = 31 - __builtin_clz(d[upward] + 2); i >= 0; i--)
if(anc(p[upward][i]) == mine) upward = p[upward][i];
if(upward != 0) {
int u = min(mybudget, budget[upward]);
mybudget -= u, budget[upward] -= u;
if(budget[upward] == 0) modnode(upward, -1), mine = anc(x);
}
}
sol += mybudget;
modnode(x, 1);
budget[x] = cost[x];
}
maxpossible = max(maxpossible, sol + specialed);
}
for(auto x : nodes) solved[x] = 1;
sol = sum - maxpossible;
return;
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> father[i] >> height[i] >> cost[i];
naive[i].emplace_back(father[i]);
naive[father[i]].emplace_back(i);
}
ll autoAdd = 0;
for(int i = 1; i <= n; i++) {
if(solved[i]) continue;
vector<int> nodes, cycles;
g[n + 1].clear(); g[0].clear();
g[0].emplace_back(n + 1); g[n + 1].emplace_back(0);
int x = i;
while(solved[x] == 0) {
solved[x] = -2;
x = father[x];
}
while(solved[x] == -2) {
solved[x] = -1;
x = father[x];
}
height[n + 1] = -inf;
cost[n + 1] = 0;
while(solved[x] == -1) {
cycles.emplace_back(x);
for(auto a : naive[x]) {
if(solved[a] != -1 && solved[a] != 1) {
fill(a, x, [&](int node) {
nodes.emplace_back(node);
if(solved[father[node]] == -1) g[node].emplace_back(n + 1), g[n + 1].emplace_back(node);
else g[node].emplace_back(father[node]), g[father[node]].emplace_back(node);
solved[node] = 1;
}, naive);
}
}
solved[x] = 1;
x = father[x];
}
DS::startProcedure(nodes, cycles);
autoAdd += DS::sol;
}
cout << autoAdd << '\n';
}
/**
Istenem! Nu poate fi real.
-- Surse verificate
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23644 KB |
Output is correct |
2 |
Correct |
4 ms |
23644 KB |
Output is correct |
3 |
Correct |
4 ms |
23644 KB |
Output is correct |
4 |
Correct |
4 ms |
23708 KB |
Output is correct |
5 |
Correct |
11 ms |
24532 KB |
Output is correct |
6 |
Correct |
8 ms |
24260 KB |
Output is correct |
7 |
Correct |
7 ms |
24152 KB |
Output is correct |
8 |
Correct |
9 ms |
24412 KB |
Output is correct |
9 |
Correct |
7 ms |
24156 KB |
Output is correct |
10 |
Correct |
7 ms |
24156 KB |
Output is correct |
11 |
Correct |
6 ms |
24156 KB |
Output is correct |
12 |
Correct |
9 ms |
24940 KB |
Output is correct |
13 |
Correct |
9 ms |
24920 KB |
Output is correct |
14 |
Correct |
9 ms |
24668 KB |
Output is correct |
15 |
Correct |
11 ms |
24748 KB |
Output is correct |
16 |
Correct |
8 ms |
24412 KB |
Output is correct |
17 |
Correct |
7 ms |
24404 KB |
Output is correct |
18 |
Correct |
6 ms |
24240 KB |
Output is correct |
19 |
Correct |
8 ms |
24776 KB |
Output is correct |
20 |
Correct |
9 ms |
24668 KB |
Output is correct |
21 |
Correct |
8 ms |
24668 KB |
Output is correct |
22 |
Correct |
10 ms |
24488 KB |
Output is correct |
23 |
Correct |
6 ms |
24156 KB |
Output is correct |
24 |
Correct |
9 ms |
24644 KB |
Output is correct |
25 |
Correct |
8 ms |
24412 KB |
Output is correct |
26 |
Correct |
8 ms |
24924 KB |
Output is correct |
27 |
Correct |
10 ms |
24668 KB |
Output is correct |
28 |
Correct |
8 ms |
24668 KB |
Output is correct |
29 |
Correct |
8 ms |
24924 KB |
Output is correct |
30 |
Correct |
7 ms |
24668 KB |
Output is correct |
31 |
Correct |
8 ms |
24664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23644 KB |
Output is correct |
2 |
Correct |
4 ms |
23644 KB |
Output is correct |
3 |
Correct |
4 ms |
23644 KB |
Output is correct |
4 |
Correct |
4 ms |
23708 KB |
Output is correct |
5 |
Correct |
11 ms |
24532 KB |
Output is correct |
6 |
Correct |
8 ms |
24260 KB |
Output is correct |
7 |
Correct |
7 ms |
24152 KB |
Output is correct |
8 |
Correct |
9 ms |
24412 KB |
Output is correct |
9 |
Correct |
7 ms |
24156 KB |
Output is correct |
10 |
Correct |
7 ms |
24156 KB |
Output is correct |
11 |
Correct |
6 ms |
24156 KB |
Output is correct |
12 |
Correct |
9 ms |
24940 KB |
Output is correct |
13 |
Correct |
9 ms |
24920 KB |
Output is correct |
14 |
Correct |
9 ms |
24668 KB |
Output is correct |
15 |
Correct |
11 ms |
24748 KB |
Output is correct |
16 |
Correct |
8 ms |
24412 KB |
Output is correct |
17 |
Correct |
7 ms |
24404 KB |
Output is correct |
18 |
Correct |
6 ms |
24240 KB |
Output is correct |
19 |
Correct |
8 ms |
24776 KB |
Output is correct |
20 |
Correct |
9 ms |
24668 KB |
Output is correct |
21 |
Correct |
8 ms |
24668 KB |
Output is correct |
22 |
Correct |
10 ms |
24488 KB |
Output is correct |
23 |
Correct |
6 ms |
24156 KB |
Output is correct |
24 |
Correct |
9 ms |
24644 KB |
Output is correct |
25 |
Correct |
8 ms |
24412 KB |
Output is correct |
26 |
Correct |
8 ms |
24924 KB |
Output is correct |
27 |
Correct |
10 ms |
24668 KB |
Output is correct |
28 |
Correct |
8 ms |
24668 KB |
Output is correct |
29 |
Correct |
8 ms |
24924 KB |
Output is correct |
30 |
Correct |
7 ms |
24668 KB |
Output is correct |
31 |
Correct |
8 ms |
24664 KB |
Output is correct |
32 |
Correct |
8 ms |
24412 KB |
Output is correct |
33 |
Correct |
402 ms |
83564 KB |
Output is correct |
34 |
Correct |
249 ms |
71328 KB |
Output is correct |
35 |
Correct |
358 ms |
83328 KB |
Output is correct |
36 |
Correct |
247 ms |
71220 KB |
Output is correct |
37 |
Correct |
184 ms |
70628 KB |
Output is correct |
38 |
Correct |
191 ms |
71240 KB |
Output is correct |
39 |
Correct |
612 ms |
98996 KB |
Output is correct |
40 |
Correct |
621 ms |
99484 KB |
Output is correct |
41 |
Correct |
383 ms |
100428 KB |
Output is correct |
42 |
Correct |
645 ms |
90900 KB |
Output is correct |
43 |
Correct |
551 ms |
89680 KB |
Output is correct |
44 |
Correct |
291 ms |
83272 KB |
Output is correct |
45 |
Correct |
181 ms |
71224 KB |
Output is correct |
46 |
Correct |
97 ms |
71716 KB |
Output is correct |
47 |
Correct |
453 ms |
93724 KB |
Output is correct |
48 |
Correct |
417 ms |
92576 KB |
Output is correct |
49 |
Correct |
274 ms |
92320 KB |
Output is correct |
50 |
Correct |
239 ms |
83400 KB |
Output is correct |
51 |
Correct |
115 ms |
71712 KB |
Output is correct |
52 |
Correct |
414 ms |
91844 KB |
Output is correct |
53 |
Correct |
358 ms |
85924 KB |
Output is correct |
54 |
Correct |
239 ms |
99488 KB |
Output is correct |
55 |
Correct |
354 ms |
93400 KB |
Output is correct |
56 |
Correct |
312 ms |
96412 KB |
Output is correct |
57 |
Correct |
294 ms |
97960 KB |
Output is correct |
58 |
Correct |
311 ms |
89476 KB |
Output is correct |
59 |
Correct |
322 ms |
91028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23644 KB |
Output is correct |
2 |
Correct |
4 ms |
23644 KB |
Output is correct |
3 |
Correct |
4 ms |
23644 KB |
Output is correct |
4 |
Correct |
4 ms |
23708 KB |
Output is correct |
5 |
Correct |
11 ms |
24532 KB |
Output is correct |
6 |
Correct |
8 ms |
24260 KB |
Output is correct |
7 |
Correct |
7 ms |
24152 KB |
Output is correct |
8 |
Correct |
9 ms |
24412 KB |
Output is correct |
9 |
Correct |
7 ms |
24156 KB |
Output is correct |
10 |
Correct |
7 ms |
24156 KB |
Output is correct |
11 |
Correct |
6 ms |
24156 KB |
Output is correct |
12 |
Correct |
9 ms |
24940 KB |
Output is correct |
13 |
Correct |
9 ms |
24920 KB |
Output is correct |
14 |
Correct |
9 ms |
24668 KB |
Output is correct |
15 |
Correct |
11 ms |
24748 KB |
Output is correct |
16 |
Correct |
8 ms |
24412 KB |
Output is correct |
17 |
Correct |
7 ms |
24404 KB |
Output is correct |
18 |
Correct |
6 ms |
24240 KB |
Output is correct |
19 |
Correct |
8 ms |
24776 KB |
Output is correct |
20 |
Correct |
9 ms |
24668 KB |
Output is correct |
21 |
Correct |
8 ms |
24668 KB |
Output is correct |
22 |
Correct |
10 ms |
24488 KB |
Output is correct |
23 |
Correct |
6 ms |
24156 KB |
Output is correct |
24 |
Correct |
9 ms |
24644 KB |
Output is correct |
25 |
Correct |
8 ms |
24412 KB |
Output is correct |
26 |
Correct |
8 ms |
24924 KB |
Output is correct |
27 |
Correct |
10 ms |
24668 KB |
Output is correct |
28 |
Correct |
8 ms |
24668 KB |
Output is correct |
29 |
Correct |
8 ms |
24924 KB |
Output is correct |
30 |
Correct |
7 ms |
24668 KB |
Output is correct |
31 |
Correct |
8 ms |
24664 KB |
Output is correct |
32 |
Correct |
8 ms |
24412 KB |
Output is correct |
33 |
Correct |
402 ms |
83564 KB |
Output is correct |
34 |
Correct |
249 ms |
71328 KB |
Output is correct |
35 |
Correct |
358 ms |
83328 KB |
Output is correct |
36 |
Correct |
247 ms |
71220 KB |
Output is correct |
37 |
Correct |
184 ms |
70628 KB |
Output is correct |
38 |
Correct |
191 ms |
71240 KB |
Output is correct |
39 |
Correct |
612 ms |
98996 KB |
Output is correct |
40 |
Correct |
621 ms |
99484 KB |
Output is correct |
41 |
Correct |
383 ms |
100428 KB |
Output is correct |
42 |
Correct |
645 ms |
90900 KB |
Output is correct |
43 |
Correct |
551 ms |
89680 KB |
Output is correct |
44 |
Correct |
291 ms |
83272 KB |
Output is correct |
45 |
Correct |
181 ms |
71224 KB |
Output is correct |
46 |
Correct |
97 ms |
71716 KB |
Output is correct |
47 |
Correct |
453 ms |
93724 KB |
Output is correct |
48 |
Correct |
417 ms |
92576 KB |
Output is correct |
49 |
Correct |
274 ms |
92320 KB |
Output is correct |
50 |
Correct |
239 ms |
83400 KB |
Output is correct |
51 |
Correct |
115 ms |
71712 KB |
Output is correct |
52 |
Correct |
414 ms |
91844 KB |
Output is correct |
53 |
Correct |
358 ms |
85924 KB |
Output is correct |
54 |
Correct |
239 ms |
99488 KB |
Output is correct |
55 |
Correct |
354 ms |
93400 KB |
Output is correct |
56 |
Correct |
312 ms |
96412 KB |
Output is correct |
57 |
Correct |
294 ms |
97960 KB |
Output is correct |
58 |
Correct |
311 ms |
89476 KB |
Output is correct |
59 |
Correct |
322 ms |
91028 KB |
Output is correct |
60 |
Correct |
4 ms |
23644 KB |
Output is correct |
61 |
Correct |
4 ms |
23644 KB |
Output is correct |
62 |
Correct |
4 ms |
23644 KB |
Output is correct |
63 |
Correct |
4 ms |
23644 KB |
Output is correct |
64 |
Correct |
416 ms |
83156 KB |
Output is correct |
65 |
Correct |
344 ms |
74304 KB |
Output is correct |
66 |
Correct |
389 ms |
78480 KB |
Output is correct |
67 |
Correct |
386 ms |
81012 KB |
Output is correct |
68 |
Correct |
239 ms |
69940 KB |
Output is correct |
69 |
Correct |
188 ms |
66216 KB |
Output is correct |
70 |
Correct |
286 ms |
50576 KB |
Output is correct |
71 |
Correct |
140 ms |
33248 KB |
Output is correct |
72 |
Correct |
318 ms |
59880 KB |
Output is correct |
73 |
Correct |
126 ms |
36072 KB |
Output is correct |
74 |
Correct |
320 ms |
85676 KB |
Output is correct |
75 |
Correct |
164 ms |
67160 KB |
Output is correct |
76 |
Correct |
161 ms |
66940 KB |
Output is correct |
77 |
Correct |
296 ms |
84920 KB |
Output is correct |
78 |
Correct |
171 ms |
66220 KB |
Output is correct |
79 |
Correct |
328 ms |
84624 KB |
Output is correct |
80 |
Correct |
203 ms |
66864 KB |
Output is correct |
81 |
Correct |
150 ms |
65632 KB |
Output is correct |
82 |
Correct |
225 ms |
59708 KB |
Output is correct |
83 |
Correct |
86 ms |
30036 KB |
Output is correct |
84 |
Correct |
304 ms |
89528 KB |
Output is correct |
85 |
Correct |
338 ms |
89516 KB |
Output is correct |
86 |
Correct |
316 ms |
83316 KB |
Output is correct |
87 |
Correct |
317 ms |
89648 KB |
Output is correct |
88 |
Correct |
320 ms |
89660 KB |
Output is correct |