#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
int n,p[200005],h[200005];
ll val[200005],Ans;
vector<int> adj[200005],adj2[400005],v[400005];
set<ll> ss[400005];
map<ll,ll> temp[400005];
bool mark[200005],vis[200005];
stack<int> st;
void dfs1(int x) {
vis[x]=true;
st.push(x);
for (auto s : adj[x]) if (!vis[s]) dfs1(s);
}
void dfs2(int x) {
for (auto s : adj2[x]) dfs2(s);
if (adj2[x].size()) {
swap(ss[x],ss[adj2[x][0]]);
swap(temp[x],temp[adj2[x][0]]);
for (int i=1; i<adj2[x].size(); ++i) {
int c=adj2[x][i];
if (ss[c].size()>ss[x].size()) swap(temp[x],temp[c]), swap(ss[x],ss[c]);
for (auto s : ss[c]) temp[x][s]+=temp[c][s], ss[x].insert(s);
}
}
if (x<=n) {
temp[x][0]+=val[x]; ss[x].insert(0);
temp[x][h[x]]-=val[x]; ss[x].insert(h[x]);
temp[x][h[x]+1]+=val[x]; ss[x].insert(h[x]+1);
if (temp[x][h[x]]<0) {
ll k=-temp[x][h[x]];
auto it=ss[x].find(h[x]);
temp[x][h[x]]=0;
auto h=it; --it;
ss[x].erase(h);
while (true) {
if (temp[x][*it]<k) {
k-=temp[x][*it];
temp[x][*it]=0;
auto h=it; --it;
ss[x].erase(h);
} else {
temp[x][*it]-=k;
if (temp[x][*it]==0) ss[x].erase(it);
break;
}
}
}
} else {
for (auto s : v[x]) {
temp[x][0]+=val[s]; ss[x].insert(0);
temp[x][h[s]]-=val[s]; ss[x].insert(h[s]);
temp[x][h[s]+1]+=val[s]; ss[x].insert(h[s]+1);
}
ll sum=0;
Ans=LLONG_MAX;
for (auto s : ss[x]) {
sum+=temp[x][s];
Ans=min(Ans,sum);
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=n; ++i) {
cin>>p[i]>>h[i]>>val[i];
adj[p[i]].push_back(i);
}
ll ans=0;
for (int i=1; i<=n; ++i) {
if (!vis[i]) {
int x=i;
dfs1(x);
while (!vis[p[x]]) {
x=p[x];
dfs1(x);
}
int idx=p[x];
while (idx!=x) {
mark[idx]=true;
v[n+x].push_back(idx);
idx=p[idx];
}
mark[x]=true;
v[n+x].push_back(x);
while (st.size()) {
int node=st.top();
st.pop();
if (mark[node]) for (auto s : adj[node]) {
if (mark[s]) continue;
if (node!=p[x] || s!=x) adj2[n+x].push_back(s);
} else for (auto s : adj[node]) adj2[node].push_back(s);
}
dfs2(n+x);
ans+=Ans;
}
}
cout<<ans;
}
Compilation message
worst_reporter2.cpp: In function 'void dfs2(int)':
worst_reporter2.cpp:27:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i=1; i<adj2[x].size(); ++i) {
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
61332 KB |
Output is correct |
2 |
Correct |
35 ms |
61376 KB |
Output is correct |
3 |
Correct |
28 ms |
61340 KB |
Output is correct |
4 |
Correct |
28 ms |
61372 KB |
Output is correct |
5 |
Correct |
41 ms |
63960 KB |
Output is correct |
6 |
Correct |
44 ms |
63248 KB |
Output is correct |
7 |
Correct |
35 ms |
62804 KB |
Output is correct |
8 |
Correct |
38 ms |
64124 KB |
Output is correct |
9 |
Correct |
35 ms |
63188 KB |
Output is correct |
10 |
Correct |
41 ms |
62748 KB |
Output is correct |
11 |
Correct |
33 ms |
62316 KB |
Output is correct |
12 |
Correct |
36 ms |
63432 KB |
Output is correct |
13 |
Correct |
32 ms |
62748 KB |
Output is correct |
14 |
Correct |
42 ms |
63060 KB |
Output is correct |
15 |
Correct |
40 ms |
62404 KB |
Output is correct |
16 |
Correct |
47 ms |
64716 KB |
Output is correct |
17 |
Correct |
37 ms |
63592 KB |
Output is correct |
18 |
Correct |
32 ms |
62284 KB |
Output is correct |
19 |
Correct |
36 ms |
63324 KB |
Output is correct |
20 |
Correct |
32 ms |
62796 KB |
Output is correct |
21 |
Correct |
32 ms |
62804 KB |
Output is correct |
22 |
Correct |
35 ms |
63288 KB |
Output is correct |
23 |
Correct |
38 ms |
62696 KB |
Output is correct |
24 |
Correct |
39 ms |
63380 KB |
Output is correct |
25 |
Correct |
31 ms |
62792 KB |
Output is correct |
26 |
Correct |
34 ms |
63480 KB |
Output is correct |
27 |
Correct |
35 ms |
63312 KB |
Output is correct |
28 |
Correct |
36 ms |
63452 KB |
Output is correct |
29 |
Correct |
45 ms |
63452 KB |
Output is correct |
30 |
Correct |
45 ms |
63464 KB |
Output is correct |
31 |
Correct |
35 ms |
63452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
61332 KB |
Output is correct |
2 |
Correct |
35 ms |
61376 KB |
Output is correct |
3 |
Correct |
28 ms |
61340 KB |
Output is correct |
4 |
Correct |
28 ms |
61372 KB |
Output is correct |
5 |
Correct |
41 ms |
63960 KB |
Output is correct |
6 |
Correct |
44 ms |
63248 KB |
Output is correct |
7 |
Correct |
35 ms |
62804 KB |
Output is correct |
8 |
Correct |
38 ms |
64124 KB |
Output is correct |
9 |
Correct |
35 ms |
63188 KB |
Output is correct |
10 |
Correct |
41 ms |
62748 KB |
Output is correct |
11 |
Correct |
33 ms |
62316 KB |
Output is correct |
12 |
Correct |
36 ms |
63432 KB |
Output is correct |
13 |
Correct |
32 ms |
62748 KB |
Output is correct |
14 |
Correct |
42 ms |
63060 KB |
Output is correct |
15 |
Correct |
40 ms |
62404 KB |
Output is correct |
16 |
Correct |
47 ms |
64716 KB |
Output is correct |
17 |
Correct |
37 ms |
63592 KB |
Output is correct |
18 |
Correct |
32 ms |
62284 KB |
Output is correct |
19 |
Correct |
36 ms |
63324 KB |
Output is correct |
20 |
Correct |
32 ms |
62796 KB |
Output is correct |
21 |
Correct |
32 ms |
62804 KB |
Output is correct |
22 |
Correct |
35 ms |
63288 KB |
Output is correct |
23 |
Correct |
38 ms |
62696 KB |
Output is correct |
24 |
Correct |
39 ms |
63380 KB |
Output is correct |
25 |
Correct |
31 ms |
62792 KB |
Output is correct |
26 |
Correct |
34 ms |
63480 KB |
Output is correct |
27 |
Correct |
35 ms |
63312 KB |
Output is correct |
28 |
Correct |
36 ms |
63452 KB |
Output is correct |
29 |
Correct |
45 ms |
63452 KB |
Output is correct |
30 |
Correct |
45 ms |
63464 KB |
Output is correct |
31 |
Correct |
35 ms |
63452 KB |
Output is correct |
32 |
Correct |
46 ms |
64044 KB |
Output is correct |
33 |
Correct |
604 ms |
192860 KB |
Output is correct |
34 |
Correct |
432 ms |
149208 KB |
Output is correct |
35 |
Correct |
655 ms |
187580 KB |
Output is correct |
36 |
Correct |
471 ms |
149184 KB |
Output is correct |
37 |
Correct |
281 ms |
111692 KB |
Output is correct |
38 |
Correct |
312 ms |
97852 KB |
Output is correct |
39 |
Correct |
571 ms |
143144 KB |
Output is correct |
40 |
Correct |
280 ms |
118300 KB |
Output is correct |
41 |
Correct |
137 ms |
118032 KB |
Output is correct |
42 |
Correct |
504 ms |
124480 KB |
Output is correct |
43 |
Correct |
283 ms |
99492 KB |
Output is correct |
44 |
Correct |
606 ms |
237660 KB |
Output is correct |
45 |
Correct |
429 ms |
175180 KB |
Output is correct |
46 |
Correct |
151 ms |
97608 KB |
Output is correct |
47 |
Correct |
624 ms |
140476 KB |
Output is correct |
48 |
Correct |
256 ms |
116624 KB |
Output is correct |
49 |
Correct |
169 ms |
116448 KB |
Output is correct |
50 |
Correct |
318 ms |
138436 KB |
Output is correct |
51 |
Correct |
171 ms |
116508 KB |
Output is correct |
52 |
Correct |
460 ms |
141348 KB |
Output is correct |
53 |
Correct |
207 ms |
117316 KB |
Output is correct |
54 |
Correct |
296 ms |
142992 KB |
Output is correct |
55 |
Correct |
497 ms |
142324 KB |
Output is correct |
56 |
Correct |
436 ms |
146556 KB |
Output is correct |
57 |
Correct |
427 ms |
148664 KB |
Output is correct |
58 |
Correct |
402 ms |
144616 KB |
Output is correct |
59 |
Correct |
409 ms |
144612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
61332 KB |
Output is correct |
2 |
Correct |
35 ms |
61376 KB |
Output is correct |
3 |
Correct |
28 ms |
61340 KB |
Output is correct |
4 |
Correct |
28 ms |
61372 KB |
Output is correct |
5 |
Correct |
41 ms |
63960 KB |
Output is correct |
6 |
Correct |
44 ms |
63248 KB |
Output is correct |
7 |
Correct |
35 ms |
62804 KB |
Output is correct |
8 |
Correct |
38 ms |
64124 KB |
Output is correct |
9 |
Correct |
35 ms |
63188 KB |
Output is correct |
10 |
Correct |
41 ms |
62748 KB |
Output is correct |
11 |
Correct |
33 ms |
62316 KB |
Output is correct |
12 |
Correct |
36 ms |
63432 KB |
Output is correct |
13 |
Correct |
32 ms |
62748 KB |
Output is correct |
14 |
Correct |
42 ms |
63060 KB |
Output is correct |
15 |
Correct |
40 ms |
62404 KB |
Output is correct |
16 |
Correct |
47 ms |
64716 KB |
Output is correct |
17 |
Correct |
37 ms |
63592 KB |
Output is correct |
18 |
Correct |
32 ms |
62284 KB |
Output is correct |
19 |
Correct |
36 ms |
63324 KB |
Output is correct |
20 |
Correct |
32 ms |
62796 KB |
Output is correct |
21 |
Correct |
32 ms |
62804 KB |
Output is correct |
22 |
Correct |
35 ms |
63288 KB |
Output is correct |
23 |
Correct |
38 ms |
62696 KB |
Output is correct |
24 |
Correct |
39 ms |
63380 KB |
Output is correct |
25 |
Correct |
31 ms |
62792 KB |
Output is correct |
26 |
Correct |
34 ms |
63480 KB |
Output is correct |
27 |
Correct |
35 ms |
63312 KB |
Output is correct |
28 |
Correct |
36 ms |
63452 KB |
Output is correct |
29 |
Correct |
45 ms |
63452 KB |
Output is correct |
30 |
Correct |
45 ms |
63464 KB |
Output is correct |
31 |
Correct |
35 ms |
63452 KB |
Output is correct |
32 |
Correct |
46 ms |
64044 KB |
Output is correct |
33 |
Correct |
604 ms |
192860 KB |
Output is correct |
34 |
Correct |
432 ms |
149208 KB |
Output is correct |
35 |
Correct |
655 ms |
187580 KB |
Output is correct |
36 |
Correct |
471 ms |
149184 KB |
Output is correct |
37 |
Correct |
281 ms |
111692 KB |
Output is correct |
38 |
Correct |
312 ms |
97852 KB |
Output is correct |
39 |
Correct |
571 ms |
143144 KB |
Output is correct |
40 |
Correct |
280 ms |
118300 KB |
Output is correct |
41 |
Correct |
137 ms |
118032 KB |
Output is correct |
42 |
Correct |
504 ms |
124480 KB |
Output is correct |
43 |
Correct |
283 ms |
99492 KB |
Output is correct |
44 |
Correct |
606 ms |
237660 KB |
Output is correct |
45 |
Correct |
429 ms |
175180 KB |
Output is correct |
46 |
Correct |
151 ms |
97608 KB |
Output is correct |
47 |
Correct |
624 ms |
140476 KB |
Output is correct |
48 |
Correct |
256 ms |
116624 KB |
Output is correct |
49 |
Correct |
169 ms |
116448 KB |
Output is correct |
50 |
Correct |
318 ms |
138436 KB |
Output is correct |
51 |
Correct |
171 ms |
116508 KB |
Output is correct |
52 |
Correct |
460 ms |
141348 KB |
Output is correct |
53 |
Correct |
207 ms |
117316 KB |
Output is correct |
54 |
Correct |
296 ms |
142992 KB |
Output is correct |
55 |
Correct |
497 ms |
142324 KB |
Output is correct |
56 |
Correct |
436 ms |
146556 KB |
Output is correct |
57 |
Correct |
427 ms |
148664 KB |
Output is correct |
58 |
Correct |
402 ms |
144616 KB |
Output is correct |
59 |
Correct |
409 ms |
144612 KB |
Output is correct |
60 |
Correct |
32 ms |
61388 KB |
Output is correct |
61 |
Correct |
30 ms |
61396 KB |
Output is correct |
62 |
Correct |
34 ms |
61468 KB |
Output is correct |
63 |
Correct |
33 ms |
61404 KB |
Output is correct |
64 |
Correct |
514 ms |
168592 KB |
Output is correct |
65 |
Correct |
427 ms |
138300 KB |
Output is correct |
66 |
Correct |
540 ms |
167496 KB |
Output is correct |
67 |
Correct |
442 ms |
138144 KB |
Output is correct |
68 |
Correct |
322 ms |
106776 KB |
Output is correct |
69 |
Correct |
269 ms |
95288 KB |
Output is correct |
70 |
Correct |
442 ms |
131624 KB |
Output is correct |
71 |
Correct |
172 ms |
86016 KB |
Output is correct |
72 |
Correct |
452 ms |
136524 KB |
Output is correct |
73 |
Correct |
173 ms |
93868 KB |
Output is correct |
74 |
Correct |
484 ms |
138128 KB |
Output is correct |
75 |
Correct |
249 ms |
105488 KB |
Output is correct |
76 |
Correct |
199 ms |
105260 KB |
Output is correct |
77 |
Correct |
410 ms |
138592 KB |
Output is correct |
78 |
Correct |
180 ms |
105768 KB |
Output is correct |
79 |
Correct |
531 ms |
165428 KB |
Output is correct |
80 |
Correct |
379 ms |
124320 KB |
Output is correct |
81 |
Correct |
247 ms |
103636 KB |
Output is correct |
82 |
Correct |
391 ms |
136792 KB |
Output is correct |
83 |
Correct |
169 ms |
148292 KB |
Output is correct |
84 |
Correct |
472 ms |
133220 KB |
Output is correct |
85 |
Correct |
449 ms |
133288 KB |
Output is correct |
86 |
Correct |
414 ms |
124288 KB |
Output is correct |
87 |
Correct |
463 ms |
133236 KB |
Output is correct |
88 |
Correct |
478 ms |
133240 KB |
Output is correct |