//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 && !cy[n]) {
//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 |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 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 |
5508 KB |
Output is correct |
6 |
Correct |
7 ms |
5324 KB |
Output is correct |
7 |
Correct |
7 ms |
5324 KB |
Output is correct |
8 |
Correct |
8 ms |
5580 KB |
Output is correct |
9 |
Correct |
7 ms |
5292 KB |
Output is correct |
10 |
Correct |
9 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
12 ms |
6308 KB |
Output is correct |
13 |
Correct |
6 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
5804 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
9 ms |
5580 KB |
Output is correct |
17 |
Correct |
8 ms |
5324 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 KB |
Output is correct |
19 |
Correct |
8 ms |
5964 KB |
Output is correct |
20 |
Correct |
7 ms |
5804 KB |
Output is correct |
21 |
Correct |
6 ms |
5708 KB |
Output is correct |
22 |
Correct |
7 ms |
5580 KB |
Output is correct |
23 |
Correct |
7 ms |
5196 KB |
Output is correct |
24 |
Correct |
8 ms |
5964 KB |
Output is correct |
25 |
Correct |
8 ms |
5800 KB |
Output is correct |
26 |
Correct |
6 ms |
6348 KB |
Output is correct |
27 |
Correct |
7 ms |
5872 KB |
Output is correct |
28 |
Correct |
7 ms |
6060 KB |
Output is correct |
29 |
Correct |
8 ms |
6316 KB |
Output is correct |
30 |
Correct |
9 ms |
6332 KB |
Output is correct |
31 |
Correct |
10 ms |
6352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 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 |
5508 KB |
Output is correct |
6 |
Correct |
7 ms |
5324 KB |
Output is correct |
7 |
Correct |
7 ms |
5324 KB |
Output is correct |
8 |
Correct |
8 ms |
5580 KB |
Output is correct |
9 |
Correct |
7 ms |
5292 KB |
Output is correct |
10 |
Correct |
9 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
12 ms |
6308 KB |
Output is correct |
13 |
Correct |
6 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
5804 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
9 ms |
5580 KB |
Output is correct |
17 |
Correct |
8 ms |
5324 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 KB |
Output is correct |
19 |
Correct |
8 ms |
5964 KB |
Output is correct |
20 |
Correct |
7 ms |
5804 KB |
Output is correct |
21 |
Correct |
6 ms |
5708 KB |
Output is correct |
22 |
Correct |
7 ms |
5580 KB |
Output is correct |
23 |
Correct |
7 ms |
5196 KB |
Output is correct |
24 |
Correct |
8 ms |
5964 KB |
Output is correct |
25 |
Correct |
8 ms |
5800 KB |
Output is correct |
26 |
Correct |
6 ms |
6348 KB |
Output is correct |
27 |
Correct |
7 ms |
5872 KB |
Output is correct |
28 |
Correct |
7 ms |
6060 KB |
Output is correct |
29 |
Correct |
8 ms |
6316 KB |
Output is correct |
30 |
Correct |
9 ms |
6332 KB |
Output is correct |
31 |
Correct |
10 ms |
6352 KB |
Output is correct |
32 |
Correct |
8 ms |
5580 KB |
Output is correct |
33 |
Correct |
331 ms |
25964 KB |
Output is correct |
34 |
Correct |
236 ms |
14060 KB |
Output is correct |
35 |
Correct |
332 ms |
23792 KB |
Output is correct |
36 |
Correct |
228 ms |
14020 KB |
Output is correct |
37 |
Correct |
163 ms |
14020 KB |
Output is correct |
38 |
Correct |
172 ms |
14044 KB |
Output is correct |
39 |
Correct |
236 ms |
54256 KB |
Output is correct |
40 |
Correct |
224 ms |
54340 KB |
Output is correct |
41 |
Correct |
131 ms |
54112 KB |
Output is correct |
42 |
Correct |
231 ms |
35480 KB |
Output is correct |
43 |
Correct |
210 ms |
35456 KB |
Output is correct |
44 |
Correct |
325 ms |
25432 KB |
Output is correct |
45 |
Correct |
215 ms |
13516 KB |
Output is correct |
46 |
Correct |
177 ms |
13508 KB |
Output is correct |
47 |
Correct |
290 ms |
38904 KB |
Output is correct |
48 |
Correct |
165 ms |
32408 KB |
Output is correct |
49 |
Correct |
125 ms |
32324 KB |
Output is correct |
50 |
Correct |
228 ms |
22964 KB |
Output is correct |
51 |
Correct |
147 ms |
11140 KB |
Output is correct |
52 |
Correct |
291 ms |
39404 KB |
Output is correct |
53 |
Correct |
165 ms |
32676 KB |
Output is correct |
54 |
Correct |
153 ms |
54208 KB |
Output is correct |
55 |
Correct |
213 ms |
41316 KB |
Output is correct |
56 |
Correct |
218 ms |
52912 KB |
Output is correct |
57 |
Correct |
240 ms |
58220 KB |
Output is correct |
58 |
Correct |
232 ms |
54080 KB |
Output is correct |
59 |
Correct |
250 ms |
54172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 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 |
5508 KB |
Output is correct |
6 |
Correct |
7 ms |
5324 KB |
Output is correct |
7 |
Correct |
7 ms |
5324 KB |
Output is correct |
8 |
Correct |
8 ms |
5580 KB |
Output is correct |
9 |
Correct |
7 ms |
5292 KB |
Output is correct |
10 |
Correct |
9 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
12 ms |
6308 KB |
Output is correct |
13 |
Correct |
6 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
5804 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
9 ms |
5580 KB |
Output is correct |
17 |
Correct |
8 ms |
5324 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 KB |
Output is correct |
19 |
Correct |
8 ms |
5964 KB |
Output is correct |
20 |
Correct |
7 ms |
5804 KB |
Output is correct |
21 |
Correct |
6 ms |
5708 KB |
Output is correct |
22 |
Correct |
7 ms |
5580 KB |
Output is correct |
23 |
Correct |
7 ms |
5196 KB |
Output is correct |
24 |
Correct |
8 ms |
5964 KB |
Output is correct |
25 |
Correct |
8 ms |
5800 KB |
Output is correct |
26 |
Correct |
6 ms |
6348 KB |
Output is correct |
27 |
Correct |
7 ms |
5872 KB |
Output is correct |
28 |
Correct |
7 ms |
6060 KB |
Output is correct |
29 |
Correct |
8 ms |
6316 KB |
Output is correct |
30 |
Correct |
9 ms |
6332 KB |
Output is correct |
31 |
Correct |
10 ms |
6352 KB |
Output is correct |
32 |
Correct |
8 ms |
5580 KB |
Output is correct |
33 |
Correct |
331 ms |
25964 KB |
Output is correct |
34 |
Correct |
236 ms |
14060 KB |
Output is correct |
35 |
Correct |
332 ms |
23792 KB |
Output is correct |
36 |
Correct |
228 ms |
14020 KB |
Output is correct |
37 |
Correct |
163 ms |
14020 KB |
Output is correct |
38 |
Correct |
172 ms |
14044 KB |
Output is correct |
39 |
Correct |
236 ms |
54256 KB |
Output is correct |
40 |
Correct |
224 ms |
54340 KB |
Output is correct |
41 |
Correct |
131 ms |
54112 KB |
Output is correct |
42 |
Correct |
231 ms |
35480 KB |
Output is correct |
43 |
Correct |
210 ms |
35456 KB |
Output is correct |
44 |
Correct |
325 ms |
25432 KB |
Output is correct |
45 |
Correct |
215 ms |
13516 KB |
Output is correct |
46 |
Correct |
177 ms |
13508 KB |
Output is correct |
47 |
Correct |
290 ms |
38904 KB |
Output is correct |
48 |
Correct |
165 ms |
32408 KB |
Output is correct |
49 |
Correct |
125 ms |
32324 KB |
Output is correct |
50 |
Correct |
228 ms |
22964 KB |
Output is correct |
51 |
Correct |
147 ms |
11140 KB |
Output is correct |
52 |
Correct |
291 ms |
39404 KB |
Output is correct |
53 |
Correct |
165 ms |
32676 KB |
Output is correct |
54 |
Correct |
153 ms |
54208 KB |
Output is correct |
55 |
Correct |
213 ms |
41316 KB |
Output is correct |
56 |
Correct |
218 ms |
52912 KB |
Output is correct |
57 |
Correct |
240 ms |
58220 KB |
Output is correct |
58 |
Correct |
232 ms |
54080 KB |
Output is correct |
59 |
Correct |
250 ms |
54172 KB |
Output is correct |
60 |
Correct |
3 ms |
4940 KB |
Output is correct |
61 |
Correct |
3 ms |
4940 KB |
Output is correct |
62 |
Correct |
3 ms |
5020 KB |
Output is correct |
63 |
Correct |
3 ms |
5028 KB |
Output is correct |
64 |
Correct |
356 ms |
25804 KB |
Output is correct |
65 |
Correct |
254 ms |
20016 KB |
Output is correct |
66 |
Correct |
311 ms |
27664 KB |
Output is correct |
67 |
Correct |
262 ms |
20548 KB |
Output is correct |
68 |
Correct |
193 ms |
19660 KB |
Output is correct |
69 |
Correct |
193 ms |
19488 KB |
Output is correct |
70 |
Correct |
338 ms |
38628 KB |
Output is correct |
71 |
Correct |
260 ms |
21572 KB |
Output is correct |
72 |
Correct |
370 ms |
46528 KB |
Output is correct |
73 |
Correct |
184 ms |
21588 KB |
Output is correct |
74 |
Correct |
384 ms |
37204 KB |
Output is correct |
75 |
Correct |
189 ms |
18748 KB |
Output is correct |
76 |
Correct |
192 ms |
18684 KB |
Output is correct |
77 |
Correct |
330 ms |
37592 KB |
Output is correct |
78 |
Correct |
182 ms |
19048 KB |
Output is correct |
79 |
Correct |
348 ms |
37236 KB |
Output is correct |
80 |
Correct |
240 ms |
20540 KB |
Output is correct |
81 |
Correct |
173 ms |
20268 KB |
Output is correct |
82 |
Correct |
292 ms |
46704 KB |
Output is correct |
83 |
Correct |
113 ms |
21412 KB |
Output is correct |
84 |
Correct |
357 ms |
53624 KB |
Output is correct |
85 |
Correct |
355 ms |
48432 KB |
Output is correct |
86 |
Correct |
300 ms |
41192 KB |
Output is correct |
87 |
Correct |
374 ms |
53404 KB |
Output is correct |
88 |
Correct |
359 ms |
51756 KB |
Output is correct |