#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 jump[nmax], p[nmax], d[nmax];
int budget[nmax];
void fill(int a, int f) {
//cerr << a << ' ' << f << '\n';
d[a] = d[f] + 1;
p[a] = jump[a] = f;
if(d[f] + d[jump[jump[f]]] == 2 * d[jump[f]]) jump[a] = jump[jump[f]];
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) {
int tmp = upward;
while(upward != 0 && anc(upward) == mine) {
if(anc(jump[upward]) == mine) upward = jump[upward];
tmp = upward;
upward = p[upward];
}
upward = tmp;
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 |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
8 ms |
24116 KB |
Output is correct |
6 |
Correct |
11 ms |
24168 KB |
Output is correct |
7 |
Correct |
7 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
24156 KB |
Output is correct |
9 |
Correct |
7 ms |
23900 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23876 KB |
Output is correct |
12 |
Correct |
9 ms |
24412 KB |
Output is correct |
13 |
Correct |
8 ms |
24480 KB |
Output is correct |
14 |
Correct |
8 ms |
24156 KB |
Output is correct |
15 |
Correct |
9 ms |
24112 KB |
Output is correct |
16 |
Correct |
8 ms |
24060 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23944 KB |
Output is correct |
19 |
Correct |
9 ms |
24412 KB |
Output is correct |
20 |
Correct |
10 ms |
24408 KB |
Output is correct |
21 |
Correct |
8 ms |
24380 KB |
Output is correct |
22 |
Correct |
7 ms |
24084 KB |
Output is correct |
23 |
Correct |
6 ms |
23900 KB |
Output is correct |
24 |
Correct |
8 ms |
24240 KB |
Output is correct |
25 |
Correct |
8 ms |
24152 KB |
Output is correct |
26 |
Correct |
7 ms |
24412 KB |
Output is correct |
27 |
Correct |
8 ms |
24156 KB |
Output is correct |
28 |
Correct |
7 ms |
24412 KB |
Output is correct |
29 |
Correct |
8 ms |
24412 KB |
Output is correct |
30 |
Correct |
7 ms |
24156 KB |
Output is correct |
31 |
Correct |
7 ms |
24156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
8 ms |
24116 KB |
Output is correct |
6 |
Correct |
11 ms |
24168 KB |
Output is correct |
7 |
Correct |
7 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
24156 KB |
Output is correct |
9 |
Correct |
7 ms |
23900 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23876 KB |
Output is correct |
12 |
Correct |
9 ms |
24412 KB |
Output is correct |
13 |
Correct |
8 ms |
24480 KB |
Output is correct |
14 |
Correct |
8 ms |
24156 KB |
Output is correct |
15 |
Correct |
9 ms |
24112 KB |
Output is correct |
16 |
Correct |
8 ms |
24060 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23944 KB |
Output is correct |
19 |
Correct |
9 ms |
24412 KB |
Output is correct |
20 |
Correct |
10 ms |
24408 KB |
Output is correct |
21 |
Correct |
8 ms |
24380 KB |
Output is correct |
22 |
Correct |
7 ms |
24084 KB |
Output is correct |
23 |
Correct |
6 ms |
23900 KB |
Output is correct |
24 |
Correct |
8 ms |
24240 KB |
Output is correct |
25 |
Correct |
8 ms |
24152 KB |
Output is correct |
26 |
Correct |
7 ms |
24412 KB |
Output is correct |
27 |
Correct |
8 ms |
24156 KB |
Output is correct |
28 |
Correct |
7 ms |
24412 KB |
Output is correct |
29 |
Correct |
8 ms |
24412 KB |
Output is correct |
30 |
Correct |
7 ms |
24156 KB |
Output is correct |
31 |
Correct |
7 ms |
24156 KB |
Output is correct |
32 |
Correct |
8 ms |
24156 KB |
Output is correct |
33 |
Correct |
308 ms |
58200 KB |
Output is correct |
34 |
Correct |
218 ms |
47264 KB |
Output is correct |
35 |
Correct |
301 ms |
58736 KB |
Output is correct |
36 |
Correct |
217 ms |
46240 KB |
Output is correct |
37 |
Correct |
159 ms |
44964 KB |
Output is correct |
38 |
Correct |
129 ms |
44968 KB |
Output is correct |
39 |
Correct |
425 ms |
75236 KB |
Output is correct |
40 |
Correct |
405 ms |
74604 KB |
Output is correct |
41 |
Correct |
221 ms |
74912 KB |
Output is correct |
42 |
Correct |
409 ms |
64576 KB |
Output is correct |
43 |
Correct |
386 ms |
64668 KB |
Output is correct |
44 |
Correct |
255 ms |
59492 KB |
Output is correct |
45 |
Correct |
147 ms |
47380 KB |
Output is correct |
46 |
Correct |
92 ms |
47268 KB |
Output is correct |
47 |
Correct |
405 ms |
67604 KB |
Output is correct |
48 |
Correct |
383 ms |
67768 KB |
Output is correct |
49 |
Correct |
211 ms |
67180 KB |
Output is correct |
50 |
Correct |
222 ms |
59036 KB |
Output is correct |
51 |
Correct |
103 ms |
46500 KB |
Output is correct |
52 |
Correct |
382 ms |
66944 KB |
Output is correct |
53 |
Correct |
347 ms |
59816 KB |
Output is correct |
54 |
Correct |
160 ms |
75356 KB |
Output is correct |
55 |
Correct |
243 ms |
67144 KB |
Output is correct |
56 |
Correct |
213 ms |
70560 KB |
Output is correct |
57 |
Correct |
187 ms |
73120 KB |
Output is correct |
58 |
Correct |
231 ms |
64416 KB |
Output is correct |
59 |
Correct |
234 ms |
64668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23132 KB |
Output is correct |
2 |
Correct |
4 ms |
23132 KB |
Output is correct |
3 |
Correct |
4 ms |
23132 KB |
Output is correct |
4 |
Correct |
4 ms |
23132 KB |
Output is correct |
5 |
Correct |
8 ms |
24116 KB |
Output is correct |
6 |
Correct |
11 ms |
24168 KB |
Output is correct |
7 |
Correct |
7 ms |
23900 KB |
Output is correct |
8 |
Correct |
8 ms |
24156 KB |
Output is correct |
9 |
Correct |
7 ms |
23900 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
6 ms |
23876 KB |
Output is correct |
12 |
Correct |
9 ms |
24412 KB |
Output is correct |
13 |
Correct |
8 ms |
24480 KB |
Output is correct |
14 |
Correct |
8 ms |
24156 KB |
Output is correct |
15 |
Correct |
9 ms |
24112 KB |
Output is correct |
16 |
Correct |
8 ms |
24060 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
6 ms |
23944 KB |
Output is correct |
19 |
Correct |
9 ms |
24412 KB |
Output is correct |
20 |
Correct |
10 ms |
24408 KB |
Output is correct |
21 |
Correct |
8 ms |
24380 KB |
Output is correct |
22 |
Correct |
7 ms |
24084 KB |
Output is correct |
23 |
Correct |
6 ms |
23900 KB |
Output is correct |
24 |
Correct |
8 ms |
24240 KB |
Output is correct |
25 |
Correct |
8 ms |
24152 KB |
Output is correct |
26 |
Correct |
7 ms |
24412 KB |
Output is correct |
27 |
Correct |
8 ms |
24156 KB |
Output is correct |
28 |
Correct |
7 ms |
24412 KB |
Output is correct |
29 |
Correct |
8 ms |
24412 KB |
Output is correct |
30 |
Correct |
7 ms |
24156 KB |
Output is correct |
31 |
Correct |
7 ms |
24156 KB |
Output is correct |
32 |
Correct |
8 ms |
24156 KB |
Output is correct |
33 |
Correct |
308 ms |
58200 KB |
Output is correct |
34 |
Correct |
218 ms |
47264 KB |
Output is correct |
35 |
Correct |
301 ms |
58736 KB |
Output is correct |
36 |
Correct |
217 ms |
46240 KB |
Output is correct |
37 |
Correct |
159 ms |
44964 KB |
Output is correct |
38 |
Correct |
129 ms |
44968 KB |
Output is correct |
39 |
Correct |
425 ms |
75236 KB |
Output is correct |
40 |
Correct |
405 ms |
74604 KB |
Output is correct |
41 |
Correct |
221 ms |
74912 KB |
Output is correct |
42 |
Correct |
409 ms |
64576 KB |
Output is correct |
43 |
Correct |
386 ms |
64668 KB |
Output is correct |
44 |
Correct |
255 ms |
59492 KB |
Output is correct |
45 |
Correct |
147 ms |
47380 KB |
Output is correct |
46 |
Correct |
92 ms |
47268 KB |
Output is correct |
47 |
Correct |
405 ms |
67604 KB |
Output is correct |
48 |
Correct |
383 ms |
67768 KB |
Output is correct |
49 |
Correct |
211 ms |
67180 KB |
Output is correct |
50 |
Correct |
222 ms |
59036 KB |
Output is correct |
51 |
Correct |
103 ms |
46500 KB |
Output is correct |
52 |
Correct |
382 ms |
66944 KB |
Output is correct |
53 |
Correct |
347 ms |
59816 KB |
Output is correct |
54 |
Correct |
160 ms |
75356 KB |
Output is correct |
55 |
Correct |
243 ms |
67144 KB |
Output is correct |
56 |
Correct |
213 ms |
70560 KB |
Output is correct |
57 |
Correct |
187 ms |
73120 KB |
Output is correct |
58 |
Correct |
231 ms |
64416 KB |
Output is correct |
59 |
Correct |
234 ms |
64668 KB |
Output is correct |
60 |
Correct |
4 ms |
23132 KB |
Output is correct |
61 |
Correct |
5 ms |
23128 KB |
Output is correct |
62 |
Correct |
4 ms |
23132 KB |
Output is correct |
63 |
Correct |
4 ms |
23132 KB |
Output is correct |
64 |
Correct |
323 ms |
57292 KB |
Output is correct |
65 |
Correct |
304 ms |
49156 KB |
Output is correct |
66 |
Correct |
295 ms |
54160 KB |
Output is correct |
67 |
Correct |
310 ms |
57588 KB |
Output is correct |
68 |
Correct |
184 ms |
45240 KB |
Output is correct |
69 |
Correct |
167 ms |
40868 KB |
Output is correct |
70 |
Correct |
279 ms |
52120 KB |
Output is correct |
71 |
Correct |
126 ms |
34792 KB |
Output is correct |
72 |
Correct |
320 ms |
61500 KB |
Output is correct |
73 |
Correct |
138 ms |
36524 KB |
Output is correct |
74 |
Correct |
303 ms |
60588 KB |
Output is correct |
75 |
Correct |
158 ms |
42144 KB |
Output is correct |
76 |
Correct |
129 ms |
42140 KB |
Output is correct |
77 |
Correct |
273 ms |
59916 KB |
Output is correct |
78 |
Correct |
120 ms |
41128 KB |
Output is correct |
79 |
Correct |
325 ms |
59420 KB |
Output is correct |
80 |
Correct |
179 ms |
41784 KB |
Output is correct |
81 |
Correct |
135 ms |
40776 KB |
Output is correct |
82 |
Correct |
243 ms |
61392 KB |
Output is correct |
83 |
Correct |
73 ms |
31568 KB |
Output is correct |
84 |
Correct |
304 ms |
64436 KB |
Output is correct |
85 |
Correct |
303 ms |
64428 KB |
Output is correct |
86 |
Correct |
300 ms |
58140 KB |
Output is correct |
87 |
Correct |
296 ms |
64564 KB |
Output is correct |
88 |
Correct |
303 ms |
64564 KB |
Output is correct |