//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
typedef map<int, ll> mp;
int to[maxn], deg[maxn];
bool cy[maxn], vis[maxn];
ll h[maxn], c[maxn];
vector<int> adj[maxn];
void comb(mp &a, mp &b) {
if (a.size() < b.size()) swap(a, b);
for (auto i:b) a[i.ff] += i.ss;
}
mp dfs(int n, int par) {
mp ret;
for (int v:adj[n]) {
if (v != par && !cy[v]) {
mp tmp = dfs(v, n);
comb(ret, tmp);
}
}
ret[1] += c[n];
ret[h[n]] -= c[n];
ret[h[n] + 1] += c[n];
if (ret[h[n]] < 0) {
//debug("rem", n, ret[h[n] + 1]);
ll val = ret[h[n]];
auto it = ret.lower_bound(h[n]);
vector<int> rem;
ret[h[n]] = 0;
rem.push_back(h[n]);
while (it != ret.begin() && val < 0) {
it = prev(it);
if (it->ss + val > 0) {
it->ss += val;
break;
} else {
val += it->ss;
it->ss = 0;
rem.push_back(it->ff);
}
}
for (int i:rem) ret.erase(ret.find(i));
}
debug(n);
for (auto i:ret) debug(i.ff, i.ss);
return ret;
}
int main() {
io
int n;
cin >> n;
for (int i = 1;i <= n;i++) cy[i] = 1;
for (int i = 1;i <= n;i++) {
cin >> to[i] >> h[i] >> c[i];
deg[to[i]]++;
adj[to[i]].push_back(i);
}
queue<int> que;
for (int i = 1;i <= n;i++) {
if (deg[i] == 0) que.push(i);
}
while (que.size()) {
int cur = que.front();que.pop();
cy[cur] = 0;
if (--deg[to[cur]] == 0) que.push(to[cur]);
}
ll ans = 0;
for (int i = 1;i <= n;i++) {
if (cy[i] && !vis[i]) {
//debug(i);
int cur = i;
mp v;
ll best = inf, val = 0;
do {
vis[cur] = 1;
mp tmp = dfs(cur, 0);
comb(v, tmp);
cur = to[cur];
} while (!vis[cur]);
if (!v[1]) best = 0;
for (auto j:v) {
val += j.ss;
//debug(j.ff, val);
best = min(best, val);
}
ans += best;
}
}
cout << ans << endl;
}
Compilation message
worst_reporter2.cpp: In function 'mp dfs(int, int)':
worst_reporter2.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 0
| ^
worst_reporter2.cpp:64:2: note: in expansion of macro 'debug'
64 | debug(n);
| ^~~~~
worst_reporter2.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 0
| ^
worst_reporter2.cpp:65:19: note: in expansion of macro 'debug'
65 | for (auto i:ret) debug(i.ff, i.ss);
| ^~~~~
worst_reporter2.cpp:65:12: warning: variable 'i' set but not used [-Wunused-but-set-variable]
65 | for (auto i:ret) debug(i.ff, i.ss);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
5580 KB |
Output is correct |
6 |
Correct |
7 ms |
5324 KB |
Output is correct |
7 |
Correct |
6 ms |
5292 KB |
Output is correct |
8 |
Correct |
8 ms |
5580 KB |
Output is correct |
9 |
Correct |
7 ms |
5292 KB |
Output is correct |
10 |
Correct |
7 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
8 ms |
6308 KB |
Output is correct |
13 |
Correct |
7 ms |
6388 KB |
Output is correct |
14 |
Correct |
10 ms |
5928 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
10 ms |
5580 KB |
Output is correct |
17 |
Correct |
8 ms |
5392 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 KB |
Output is correct |
19 |
Correct |
9 ms |
5968 KB |
Output is correct |
20 |
Correct |
6 ms |
5836 KB |
Output is correct |
21 |
Correct |
6 ms |
5836 KB |
Output is correct |
22 |
Correct |
6 ms |
5580 KB |
Output is correct |
23 |
Correct |
6 ms |
5288 KB |
Output is correct |
24 |
Correct |
8 ms |
5936 KB |
Output is correct |
25 |
Correct |
6 ms |
5816 KB |
Output is correct |
26 |
Correct |
6 ms |
6312 KB |
Output is correct |
27 |
Correct |
7 ms |
5836 KB |
Output is correct |
28 |
Correct |
8 ms |
6124 KB |
Output is correct |
29 |
Correct |
7 ms |
6348 KB |
Output is correct |
30 |
Correct |
8 ms |
6348 KB |
Output is correct |
31 |
Correct |
8 ms |
6348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
5580 KB |
Output is correct |
6 |
Correct |
7 ms |
5324 KB |
Output is correct |
7 |
Correct |
6 ms |
5292 KB |
Output is correct |
8 |
Correct |
8 ms |
5580 KB |
Output is correct |
9 |
Correct |
7 ms |
5292 KB |
Output is correct |
10 |
Correct |
7 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
8 ms |
6308 KB |
Output is correct |
13 |
Correct |
7 ms |
6388 KB |
Output is correct |
14 |
Correct |
10 ms |
5928 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
10 ms |
5580 KB |
Output is correct |
17 |
Correct |
8 ms |
5392 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 KB |
Output is correct |
19 |
Correct |
9 ms |
5968 KB |
Output is correct |
20 |
Correct |
6 ms |
5836 KB |
Output is correct |
21 |
Correct |
6 ms |
5836 KB |
Output is correct |
22 |
Correct |
6 ms |
5580 KB |
Output is correct |
23 |
Correct |
6 ms |
5288 KB |
Output is correct |
24 |
Correct |
8 ms |
5936 KB |
Output is correct |
25 |
Correct |
6 ms |
5816 KB |
Output is correct |
26 |
Correct |
6 ms |
6312 KB |
Output is correct |
27 |
Correct |
7 ms |
5836 KB |
Output is correct |
28 |
Correct |
8 ms |
6124 KB |
Output is correct |
29 |
Correct |
7 ms |
6348 KB |
Output is correct |
30 |
Correct |
8 ms |
6348 KB |
Output is correct |
31 |
Correct |
8 ms |
6348 KB |
Output is correct |
32 |
Correct |
9 ms |
5580 KB |
Output is correct |
33 |
Correct |
313 ms |
26208 KB |
Output is correct |
34 |
Correct |
225 ms |
14300 KB |
Output is correct |
35 |
Correct |
311 ms |
24112 KB |
Output is correct |
36 |
Correct |
231 ms |
14148 KB |
Output is correct |
37 |
Correct |
162 ms |
14188 KB |
Output is correct |
38 |
Correct |
157 ms |
14220 KB |
Output is correct |
39 |
Correct |
226 ms |
54468 KB |
Output is correct |
40 |
Correct |
213 ms |
54456 KB |
Output is correct |
41 |
Correct |
119 ms |
54424 KB |
Output is correct |
42 |
Correct |
226 ms |
35864 KB |
Output is correct |
43 |
Correct |
204 ms |
35612 KB |
Output is correct |
44 |
Correct |
306 ms |
25716 KB |
Output is correct |
45 |
Correct |
204 ms |
13908 KB |
Output is correct |
46 |
Correct |
111 ms |
13744 KB |
Output is correct |
47 |
Correct |
283 ms |
39184 KB |
Output is correct |
48 |
Correct |
160 ms |
32552 KB |
Output is correct |
49 |
Correct |
118 ms |
32428 KB |
Output is correct |
50 |
Correct |
207 ms |
23188 KB |
Output is correct |
51 |
Correct |
112 ms |
11488 KB |
Output is correct |
52 |
Correct |
268 ms |
39700 KB |
Output is correct |
53 |
Correct |
151 ms |
32872 KB |
Output is correct |
54 |
Correct |
139 ms |
54420 KB |
Output is correct |
55 |
Correct |
221 ms |
41436 KB |
Output is correct |
56 |
Correct |
210 ms |
53060 KB |
Output is correct |
57 |
Correct |
219 ms |
58380 KB |
Output is correct |
58 |
Correct |
252 ms |
54420 KB |
Output is correct |
59 |
Correct |
245 ms |
54384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
5580 KB |
Output is correct |
6 |
Correct |
7 ms |
5324 KB |
Output is correct |
7 |
Correct |
6 ms |
5292 KB |
Output is correct |
8 |
Correct |
8 ms |
5580 KB |
Output is correct |
9 |
Correct |
7 ms |
5292 KB |
Output is correct |
10 |
Correct |
7 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
8 ms |
6308 KB |
Output is correct |
13 |
Correct |
7 ms |
6388 KB |
Output is correct |
14 |
Correct |
10 ms |
5928 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
10 ms |
5580 KB |
Output is correct |
17 |
Correct |
8 ms |
5392 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 KB |
Output is correct |
19 |
Correct |
9 ms |
5968 KB |
Output is correct |
20 |
Correct |
6 ms |
5836 KB |
Output is correct |
21 |
Correct |
6 ms |
5836 KB |
Output is correct |
22 |
Correct |
6 ms |
5580 KB |
Output is correct |
23 |
Correct |
6 ms |
5288 KB |
Output is correct |
24 |
Correct |
8 ms |
5936 KB |
Output is correct |
25 |
Correct |
6 ms |
5816 KB |
Output is correct |
26 |
Correct |
6 ms |
6312 KB |
Output is correct |
27 |
Correct |
7 ms |
5836 KB |
Output is correct |
28 |
Correct |
8 ms |
6124 KB |
Output is correct |
29 |
Correct |
7 ms |
6348 KB |
Output is correct |
30 |
Correct |
8 ms |
6348 KB |
Output is correct |
31 |
Correct |
8 ms |
6348 KB |
Output is correct |
32 |
Correct |
9 ms |
5580 KB |
Output is correct |
33 |
Correct |
313 ms |
26208 KB |
Output is correct |
34 |
Correct |
225 ms |
14300 KB |
Output is correct |
35 |
Correct |
311 ms |
24112 KB |
Output is correct |
36 |
Correct |
231 ms |
14148 KB |
Output is correct |
37 |
Correct |
162 ms |
14188 KB |
Output is correct |
38 |
Correct |
157 ms |
14220 KB |
Output is correct |
39 |
Correct |
226 ms |
54468 KB |
Output is correct |
40 |
Correct |
213 ms |
54456 KB |
Output is correct |
41 |
Correct |
119 ms |
54424 KB |
Output is correct |
42 |
Correct |
226 ms |
35864 KB |
Output is correct |
43 |
Correct |
204 ms |
35612 KB |
Output is correct |
44 |
Correct |
306 ms |
25716 KB |
Output is correct |
45 |
Correct |
204 ms |
13908 KB |
Output is correct |
46 |
Correct |
111 ms |
13744 KB |
Output is correct |
47 |
Correct |
283 ms |
39184 KB |
Output is correct |
48 |
Correct |
160 ms |
32552 KB |
Output is correct |
49 |
Correct |
118 ms |
32428 KB |
Output is correct |
50 |
Correct |
207 ms |
23188 KB |
Output is correct |
51 |
Correct |
112 ms |
11488 KB |
Output is correct |
52 |
Correct |
268 ms |
39700 KB |
Output is correct |
53 |
Correct |
151 ms |
32872 KB |
Output is correct |
54 |
Correct |
139 ms |
54420 KB |
Output is correct |
55 |
Correct |
221 ms |
41436 KB |
Output is correct |
56 |
Correct |
210 ms |
53060 KB |
Output is correct |
57 |
Correct |
219 ms |
58380 KB |
Output is correct |
58 |
Correct |
252 ms |
54420 KB |
Output is correct |
59 |
Correct |
245 ms |
54384 KB |
Output is correct |
60 |
Incorrect |
3 ms |
5024 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |