#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
const int mxN = 2e5+5;
int N, A[mxN], H[mxN], C[mxN], V;
vector<int> al[mxN], vals, cyc;
int vis[mxN], pa[mxN], onstk[mxN], incyc[mxN];
map<int,ll> dp[mxN];
void dfs(int u) {
dp[u].insert(make_pair(0,0));
for (int v : al[u]) {
dfs(v);
if (dp[v].count(H[v]+1)) dp[v][H[v]+1] += C[v];
else dp[v].insert(make_pair(H[v]+1, C[v]));
if (0 < H[v]) {
if (dp[v].count(0)) dp[v][0] += C[v];
else dp[v].insert(make_pair(0,C[v]));
auto it = --dp[v].lower_bound(H[v]+1);
while (it != dp[v].begin() && it->second <= C[v]) {
auto x = prev(it);
x->second += it->second;
dp[v].erase(it);
it = x;
}
it->second -= C[v];
}
if (SZ(dp[v]) > SZ(dp[u])) swap(dp[v],dp[u]);
for (auto& x : dp[v]) {
if (dp[u].count(x.first)) dp[u][x.first] += x.second;
else dp[u].insert(x);
}
}
}
void dfs2(int u) {
vis[u] = 1;
onstk[u] = 1;
for (int& v : al[u]) {
if (!vis[v]) {
pa[v] = u;
dfs2(v);
} else if (onstk[v]) {
cyc.push_back(v);
for (int x = u; x != v; x = pa[x]) cyc.push_back(x);
}
}
onstk[u] = 0;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
FOR(i,1,N){
cin >> A[i] >> H[i] >> C[i];
al[A[i]].push_back(i);
vals.push_back(H[i]);
}
vals.push_back(1);
sort(ALL(vals));
vals.resize(unique(ALL(vals))-vals.begin());
FOR(i,1,N){
H[i] = lower_bound(ALL(vals),H[i])-vals.begin();
}
V = SZ(vals);
ll tot = 0;
memset(vis,0,sizeof vis);
FOR(i,1,N) if (!vis[i]) {
cyc.clear();
dfs2(i);
if (cyc.empty()) continue;
sort(ALL(cyc), [](int u, int v){ return H[u] < H[v]; });
for (int& u : cyc) incyc[u] = 1;
auto& adj = al[cyc[0]];
RFOR(j,SZ(adj)-1,0) if (incyc[adj[j]]) {
adj.erase(adj.begin()+j);
}
FOR(j,1,SZ(cyc)-1){
for (int& v : al[cyc[j]]) if (!incyc[v]) {
adj.push_back(v);
}
al[cyc[j]].clear();
al[cyc[j]].push_back(cyc[j-1]);
}
int root = cyc.back();
dfs(root);
ll ans = dp[root][0] + C[root], cur = 0;
for (auto& x : dp[root]) {
if (x.first > H[root]) break;
cur += x.second;
}
ans = min(ans, cur);
//TRACE(ans);
tot += ans;
}
cout << tot << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15180 KB |
Output is correct |
2 |
Correct |
10 ms |
15180 KB |
Output is correct |
3 |
Correct |
8 ms |
15208 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
16 ms |
16772 KB |
Output is correct |
6 |
Correct |
15 ms |
16376 KB |
Output is correct |
7 |
Correct |
13 ms |
16168 KB |
Output is correct |
8 |
Correct |
17 ms |
16880 KB |
Output is correct |
9 |
Correct |
15 ms |
16332 KB |
Output is correct |
10 |
Correct |
13 ms |
16060 KB |
Output is correct |
11 |
Correct |
14 ms |
15948 KB |
Output is correct |
12 |
Correct |
14 ms |
16792 KB |
Output is correct |
13 |
Correct |
12 ms |
16736 KB |
Output is correct |
14 |
Correct |
14 ms |
16332 KB |
Output is correct |
15 |
Correct |
14 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
17116 KB |
Output is correct |
17 |
Correct |
15 ms |
16480 KB |
Output is correct |
18 |
Correct |
12 ms |
15960 KB |
Output is correct |
19 |
Correct |
14 ms |
16588 KB |
Output is correct |
20 |
Correct |
13 ms |
16332 KB |
Output is correct |
21 |
Correct |
12 ms |
16332 KB |
Output is correct |
22 |
Correct |
13 ms |
16416 KB |
Output is correct |
23 |
Correct |
12 ms |
16112 KB |
Output is correct |
24 |
Correct |
14 ms |
16604 KB |
Output is correct |
25 |
Correct |
13 ms |
16348 KB |
Output is correct |
26 |
Correct |
13 ms |
16788 KB |
Output is correct |
27 |
Correct |
15 ms |
16604 KB |
Output is correct |
28 |
Correct |
17 ms |
16600 KB |
Output is correct |
29 |
Correct |
16 ms |
16844 KB |
Output is correct |
30 |
Correct |
15 ms |
16728 KB |
Output is correct |
31 |
Correct |
14 ms |
16736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15180 KB |
Output is correct |
2 |
Correct |
10 ms |
15180 KB |
Output is correct |
3 |
Correct |
8 ms |
15208 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
16 ms |
16772 KB |
Output is correct |
6 |
Correct |
15 ms |
16376 KB |
Output is correct |
7 |
Correct |
13 ms |
16168 KB |
Output is correct |
8 |
Correct |
17 ms |
16880 KB |
Output is correct |
9 |
Correct |
15 ms |
16332 KB |
Output is correct |
10 |
Correct |
13 ms |
16060 KB |
Output is correct |
11 |
Correct |
14 ms |
15948 KB |
Output is correct |
12 |
Correct |
14 ms |
16792 KB |
Output is correct |
13 |
Correct |
12 ms |
16736 KB |
Output is correct |
14 |
Correct |
14 ms |
16332 KB |
Output is correct |
15 |
Correct |
14 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
17116 KB |
Output is correct |
17 |
Correct |
15 ms |
16480 KB |
Output is correct |
18 |
Correct |
12 ms |
15960 KB |
Output is correct |
19 |
Correct |
14 ms |
16588 KB |
Output is correct |
20 |
Correct |
13 ms |
16332 KB |
Output is correct |
21 |
Correct |
12 ms |
16332 KB |
Output is correct |
22 |
Correct |
13 ms |
16416 KB |
Output is correct |
23 |
Correct |
12 ms |
16112 KB |
Output is correct |
24 |
Correct |
14 ms |
16604 KB |
Output is correct |
25 |
Correct |
13 ms |
16348 KB |
Output is correct |
26 |
Correct |
13 ms |
16788 KB |
Output is correct |
27 |
Correct |
15 ms |
16604 KB |
Output is correct |
28 |
Correct |
17 ms |
16600 KB |
Output is correct |
29 |
Correct |
16 ms |
16844 KB |
Output is correct |
30 |
Correct |
15 ms |
16728 KB |
Output is correct |
31 |
Correct |
14 ms |
16736 KB |
Output is correct |
32 |
Correct |
16 ms |
16736 KB |
Output is correct |
33 |
Correct |
490 ms |
95104 KB |
Output is correct |
34 |
Correct |
370 ms |
70324 KB |
Output is correct |
35 |
Correct |
504 ms |
92084 KB |
Output is correct |
36 |
Correct |
381 ms |
70216 KB |
Output is correct |
37 |
Correct |
265 ms |
51256 KB |
Output is correct |
38 |
Correct |
233 ms |
46648 KB |
Output is correct |
39 |
Correct |
252 ms |
78260 KB |
Output is correct |
40 |
Correct |
228 ms |
78208 KB |
Output is correct |
41 |
Correct |
166 ms |
78176 KB |
Output is correct |
42 |
Correct |
247 ms |
61192 KB |
Output is correct |
43 |
Correct |
221 ms |
60980 KB |
Output is correct |
44 |
Correct |
490 ms |
119964 KB |
Output is correct |
45 |
Correct |
334 ms |
84424 KB |
Output is correct |
46 |
Correct |
154 ms |
46956 KB |
Output is correct |
47 |
Correct |
360 ms |
71020 KB |
Output is correct |
48 |
Correct |
216 ms |
64216 KB |
Output is correct |
49 |
Correct |
161 ms |
63996 KB |
Output is correct |
50 |
Correct |
325 ms |
62380 KB |
Output is correct |
51 |
Correct |
175 ms |
49940 KB |
Output is correct |
52 |
Correct |
395 ms |
71688 KB |
Output is correct |
53 |
Correct |
201 ms |
64572 KB |
Output is correct |
54 |
Correct |
172 ms |
78436 KB |
Output is correct |
55 |
Correct |
323 ms |
72592 KB |
Output is correct |
56 |
Correct |
302 ms |
80056 KB |
Output is correct |
57 |
Correct |
289 ms |
83580 KB |
Output is correct |
58 |
Correct |
311 ms |
79032 KB |
Output is correct |
59 |
Correct |
322 ms |
79008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15180 KB |
Output is correct |
2 |
Correct |
10 ms |
15180 KB |
Output is correct |
3 |
Correct |
8 ms |
15208 KB |
Output is correct |
4 |
Correct |
9 ms |
15180 KB |
Output is correct |
5 |
Correct |
16 ms |
16772 KB |
Output is correct |
6 |
Correct |
15 ms |
16376 KB |
Output is correct |
7 |
Correct |
13 ms |
16168 KB |
Output is correct |
8 |
Correct |
17 ms |
16880 KB |
Output is correct |
9 |
Correct |
15 ms |
16332 KB |
Output is correct |
10 |
Correct |
13 ms |
16060 KB |
Output is correct |
11 |
Correct |
14 ms |
15948 KB |
Output is correct |
12 |
Correct |
14 ms |
16792 KB |
Output is correct |
13 |
Correct |
12 ms |
16736 KB |
Output is correct |
14 |
Correct |
14 ms |
16332 KB |
Output is correct |
15 |
Correct |
14 ms |
16292 KB |
Output is correct |
16 |
Correct |
17 ms |
17116 KB |
Output is correct |
17 |
Correct |
15 ms |
16480 KB |
Output is correct |
18 |
Correct |
12 ms |
15960 KB |
Output is correct |
19 |
Correct |
14 ms |
16588 KB |
Output is correct |
20 |
Correct |
13 ms |
16332 KB |
Output is correct |
21 |
Correct |
12 ms |
16332 KB |
Output is correct |
22 |
Correct |
13 ms |
16416 KB |
Output is correct |
23 |
Correct |
12 ms |
16112 KB |
Output is correct |
24 |
Correct |
14 ms |
16604 KB |
Output is correct |
25 |
Correct |
13 ms |
16348 KB |
Output is correct |
26 |
Correct |
13 ms |
16788 KB |
Output is correct |
27 |
Correct |
15 ms |
16604 KB |
Output is correct |
28 |
Correct |
17 ms |
16600 KB |
Output is correct |
29 |
Correct |
16 ms |
16844 KB |
Output is correct |
30 |
Correct |
15 ms |
16728 KB |
Output is correct |
31 |
Correct |
14 ms |
16736 KB |
Output is correct |
32 |
Correct |
16 ms |
16736 KB |
Output is correct |
33 |
Correct |
490 ms |
95104 KB |
Output is correct |
34 |
Correct |
370 ms |
70324 KB |
Output is correct |
35 |
Correct |
504 ms |
92084 KB |
Output is correct |
36 |
Correct |
381 ms |
70216 KB |
Output is correct |
37 |
Correct |
265 ms |
51256 KB |
Output is correct |
38 |
Correct |
233 ms |
46648 KB |
Output is correct |
39 |
Correct |
252 ms |
78260 KB |
Output is correct |
40 |
Correct |
228 ms |
78208 KB |
Output is correct |
41 |
Correct |
166 ms |
78176 KB |
Output is correct |
42 |
Correct |
247 ms |
61192 KB |
Output is correct |
43 |
Correct |
221 ms |
60980 KB |
Output is correct |
44 |
Correct |
490 ms |
119964 KB |
Output is correct |
45 |
Correct |
334 ms |
84424 KB |
Output is correct |
46 |
Correct |
154 ms |
46956 KB |
Output is correct |
47 |
Correct |
360 ms |
71020 KB |
Output is correct |
48 |
Correct |
216 ms |
64216 KB |
Output is correct |
49 |
Correct |
161 ms |
63996 KB |
Output is correct |
50 |
Correct |
325 ms |
62380 KB |
Output is correct |
51 |
Correct |
175 ms |
49940 KB |
Output is correct |
52 |
Correct |
395 ms |
71688 KB |
Output is correct |
53 |
Correct |
201 ms |
64572 KB |
Output is correct |
54 |
Correct |
172 ms |
78436 KB |
Output is correct |
55 |
Correct |
323 ms |
72592 KB |
Output is correct |
56 |
Correct |
302 ms |
80056 KB |
Output is correct |
57 |
Correct |
289 ms |
83580 KB |
Output is correct |
58 |
Correct |
311 ms |
79032 KB |
Output is correct |
59 |
Correct |
322 ms |
79008 KB |
Output is correct |
60 |
Correct |
8 ms |
15192 KB |
Output is correct |
61 |
Correct |
8 ms |
15196 KB |
Output is correct |
62 |
Correct |
8 ms |
15140 KB |
Output is correct |
63 |
Correct |
8 ms |
15180 KB |
Output is correct |
64 |
Correct |
459 ms |
80696 KB |
Output is correct |
65 |
Correct |
394 ms |
63952 KB |
Output is correct |
66 |
Correct |
473 ms |
81300 KB |
Output is correct |
67 |
Correct |
371 ms |
63580 KB |
Output is correct |
68 |
Correct |
294 ms |
50488 KB |
Output is correct |
69 |
Correct |
253 ms |
47024 KB |
Output is correct |
70 |
Correct |
393 ms |
68748 KB |
Output is correct |
71 |
Correct |
359 ms |
62528 KB |
Output is correct |
72 |
Correct |
395 ms |
79756 KB |
Output is correct |
73 |
Correct |
357 ms |
79912 KB |
Output is correct |
74 |
Correct |
504 ms |
73380 KB |
Output is correct |
75 |
Correct |
343 ms |
65720 KB |
Output is correct |
76 |
Correct |
309 ms |
65716 KB |
Output is correct |
77 |
Correct |
438 ms |
73856 KB |
Output is correct |
78 |
Correct |
288 ms |
66100 KB |
Output is correct |
79 |
Correct |
499 ms |
88864 KB |
Output is correct |
80 |
Correct |
418 ms |
76816 KB |
Output is correct |
81 |
Correct |
328 ms |
66104 KB |
Output is correct |
82 |
Correct |
409 ms |
80080 KB |
Output is correct |
83 |
Correct |
164 ms |
43704 KB |
Output is correct |
84 |
Correct |
502 ms |
78060 KB |
Output is correct |
85 |
Correct |
476 ms |
78104 KB |
Output is correct |
86 |
Correct |
495 ms |
78044 KB |
Output is correct |
87 |
Correct |
473 ms |
78004 KB |
Output is correct |
88 |
Correct |
495 ms |
78084 KB |
Output is correct |