#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
pair<int, pair<int, int>> edges[200000];
int cmp[300000], deg[300000], cost[300000], nodes;
bool has_deg_3[300000], has_cycle[300000];
vector<int> dsu_tree[300000];
int tin[300000], tout[300000], timer = 0, anc[300000][20];
int find(int A) {
while (A != cmp[A]) A = cmp[A], cmp[A] = cmp[cmp[A]];
return A;
}
void onion(int A, int B) {
has_cycle[nodes] = has_cycle[find(A)] || has_cycle[find(B)];
if (find(A) == find(B)) has_cycle[nodes] = true;
has_deg_3[nodes] = has_deg_3[find(A)] || has_deg_3[find(B)];
if (deg[A] == 3 || deg[B] == 3) has_deg_3[nodes] = true;
dsu_tree[nodes].push_back(find(A));
if (find(A) != find(B)) dsu_tree[nodes].push_back(find(B));
cmp[find(A)] = cmp[find(B)] = nodes;
nodes++;
}
void dfs(int node = nodes - 1, int parent = -1) {
tin[node] = timer++;
for (int i = 1; i < 20; i++) anc[node][i] = -1;
for (int i = 1; i < 20 && ~anc[node][i - 1]; i++) anc[node][i] = anc[anc[node][i - 1]][i - 1];
for (int i : dsu_tree[node]) {
anc[i][0] = node;
dfs(i, node);
}
tout[node] = timer++;
}
bool is_ancestor(int A, int B) { return tin[A] <= tin[B] && tout[A] >= tout[B]; }
int lca(int A, int B) {
if (is_ancestor(A, B)) return A;
for (int i = 19; ~i; i--) if (~anc[A][i] && !is_ancestor(anc[A][i], B)) A = anc[A][i];
return anc[A][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
for (int i = 0; i < M; i++) edges[i] = {W[i], {U[i], V[i]}};
sort(edges, edges + M);
nodes = N;
iota(cmp, cmp + N + M, 0);
for (int i = 0; i < M; i++) {
deg[edges[i].second.first]++;
deg[edges[i].second.second]++;
cost[nodes] = edges[i].first;
onion(edges[i].second.first, edges[i].second.second);
}
for (int i = 0; i < nodes; i++) for (int j = 0; j < 20; j++) anc[i][j] = -1;
dfs();
}
int getMinimumFuelCapacity(int X, int Y) {
if (!has_cycle[nodes - 1] && !has_deg_3[nodes - 1]) return -1;
int l = lca(X, Y);
if (has_cycle[l] || has_deg_3[l]) return cost[l];
for (int i = 19; ~i; i--) if (~anc[l][i] && !has_cycle[anc[l][i]] && !has_deg_3[anc[l][i]])
l = anc[l][i];
return cost[anc[l][0]];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7552 KB |
Output is correct |
5 |
Correct |
6 ms |
7680 KB |
Output is correct |
6 |
Correct |
7 ms |
7632 KB |
Output is correct |
7 |
Correct |
7 ms |
7680 KB |
Output is correct |
8 |
Correct |
7 ms |
7680 KB |
Output is correct |
9 |
Correct |
124 ms |
27512 KB |
Output is correct |
10 |
Correct |
148 ms |
31992 KB |
Output is correct |
11 |
Correct |
147 ms |
31608 KB |
Output is correct |
12 |
Correct |
160 ms |
33016 KB |
Output is correct |
13 |
Correct |
143 ms |
35356 KB |
Output is correct |
14 |
Correct |
125 ms |
27640 KB |
Output is correct |
15 |
Correct |
224 ms |
34052 KB |
Output is correct |
16 |
Correct |
231 ms |
33320 KB |
Output is correct |
17 |
Correct |
239 ms |
35040 KB |
Output is correct |
18 |
Correct |
221 ms |
37212 KB |
Output is correct |
19 |
Correct |
111 ms |
14968 KB |
Output is correct |
20 |
Correct |
261 ms |
35352 KB |
Output is correct |
21 |
Correct |
260 ms |
34608 KB |
Output is correct |
22 |
Correct |
277 ms |
36320 KB |
Output is correct |
23 |
Correct |
386 ms |
38624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
343 ms |
39216 KB |
Output is correct |
4 |
Correct |
339 ms |
40572 KB |
Output is correct |
5 |
Correct |
350 ms |
39824 KB |
Output is correct |
6 |
Correct |
360 ms |
40340 KB |
Output is correct |
7 |
Correct |
356 ms |
40180 KB |
Output is correct |
8 |
Correct |
347 ms |
39076 KB |
Output is correct |
9 |
Correct |
340 ms |
39876 KB |
Output is correct |
10 |
Correct |
335 ms |
38708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7552 KB |
Output is correct |
6 |
Correct |
6 ms |
7680 KB |
Output is correct |
7 |
Correct |
7 ms |
7632 KB |
Output is correct |
8 |
Correct |
7 ms |
7680 KB |
Output is correct |
9 |
Correct |
7 ms |
7680 KB |
Output is correct |
10 |
Correct |
6 ms |
7680 KB |
Output is correct |
11 |
Correct |
6 ms |
7680 KB |
Output is correct |
12 |
Correct |
6 ms |
7680 KB |
Output is correct |
13 |
Correct |
6 ms |
7552 KB |
Output is correct |
14 |
Correct |
6 ms |
7680 KB |
Output is correct |
15 |
Correct |
7 ms |
7680 KB |
Output is correct |
16 |
Correct |
6 ms |
7680 KB |
Output is correct |
17 |
Correct |
6 ms |
7808 KB |
Output is correct |
18 |
Correct |
6 ms |
7680 KB |
Output is correct |
19 |
Correct |
6 ms |
7680 KB |
Output is correct |
20 |
Correct |
6 ms |
7680 KB |
Output is correct |
21 |
Correct |
7 ms |
7680 KB |
Output is correct |
22 |
Correct |
7 ms |
7808 KB |
Output is correct |
23 |
Correct |
6 ms |
7552 KB |
Output is correct |
24 |
Correct |
8 ms |
7808 KB |
Output is correct |
25 |
Correct |
7 ms |
7808 KB |
Output is correct |
26 |
Correct |
7 ms |
7808 KB |
Output is correct |
27 |
Correct |
6 ms |
7680 KB |
Output is correct |
28 |
Correct |
7 ms |
7808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7552 KB |
Output is correct |
6 |
Correct |
6 ms |
7680 KB |
Output is correct |
7 |
Correct |
7 ms |
7632 KB |
Output is correct |
8 |
Correct |
7 ms |
7680 KB |
Output is correct |
9 |
Correct |
7 ms |
7680 KB |
Output is correct |
10 |
Correct |
124 ms |
27512 KB |
Output is correct |
11 |
Correct |
148 ms |
31992 KB |
Output is correct |
12 |
Correct |
147 ms |
31608 KB |
Output is correct |
13 |
Correct |
160 ms |
33016 KB |
Output is correct |
14 |
Correct |
143 ms |
35356 KB |
Output is correct |
15 |
Correct |
6 ms |
7680 KB |
Output is correct |
16 |
Correct |
6 ms |
7680 KB |
Output is correct |
17 |
Correct |
6 ms |
7680 KB |
Output is correct |
18 |
Correct |
6 ms |
7552 KB |
Output is correct |
19 |
Correct |
6 ms |
7680 KB |
Output is correct |
20 |
Correct |
7 ms |
7680 KB |
Output is correct |
21 |
Correct |
6 ms |
7680 KB |
Output is correct |
22 |
Correct |
6 ms |
7808 KB |
Output is correct |
23 |
Correct |
6 ms |
7680 KB |
Output is correct |
24 |
Correct |
20 ms |
10880 KB |
Output is correct |
25 |
Correct |
149 ms |
33016 KB |
Output is correct |
26 |
Correct |
154 ms |
33020 KB |
Output is correct |
27 |
Correct |
148 ms |
33016 KB |
Output is correct |
28 |
Correct |
147 ms |
32760 KB |
Output is correct |
29 |
Correct |
145 ms |
32632 KB |
Output is correct |
30 |
Correct |
138 ms |
30456 KB |
Output is correct |
31 |
Correct |
155 ms |
33144 KB |
Output is correct |
32 |
Correct |
150 ms |
33200 KB |
Output is correct |
33 |
Correct |
137 ms |
36120 KB |
Output is correct |
34 |
Correct |
156 ms |
33016 KB |
Output is correct |
35 |
Correct |
6 ms |
7680 KB |
Output is correct |
36 |
Correct |
6 ms |
7680 KB |
Output is correct |
37 |
Correct |
7 ms |
7680 KB |
Output is correct |
38 |
Correct |
7 ms |
7808 KB |
Output is correct |
39 |
Correct |
6 ms |
7552 KB |
Output is correct |
40 |
Correct |
8 ms |
7808 KB |
Output is correct |
41 |
Correct |
7 ms |
7808 KB |
Output is correct |
42 |
Correct |
7 ms |
7808 KB |
Output is correct |
43 |
Correct |
6 ms |
7680 KB |
Output is correct |
44 |
Correct |
7 ms |
7808 KB |
Output is correct |
45 |
Correct |
215 ms |
43000 KB |
Output is correct |
46 |
Correct |
146 ms |
33016 KB |
Output is correct |
47 |
Correct |
156 ms |
33144 KB |
Output is correct |
48 |
Correct |
170 ms |
36472 KB |
Output is correct |
49 |
Correct |
160 ms |
40568 KB |
Output is correct |
50 |
Correct |
131 ms |
33144 KB |
Output is correct |
51 |
Correct |
180 ms |
38256 KB |
Output is correct |
52 |
Correct |
233 ms |
45944 KB |
Output is correct |
53 |
Correct |
271 ms |
48504 KB |
Output is correct |
54 |
Correct |
280 ms |
54008 KB |
Output is correct |
55 |
Correct |
140 ms |
35960 KB |
Output is correct |
56 |
Correct |
256 ms |
49528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7552 KB |
Output is correct |
5 |
Correct |
6 ms |
7680 KB |
Output is correct |
6 |
Correct |
7 ms |
7632 KB |
Output is correct |
7 |
Correct |
7 ms |
7680 KB |
Output is correct |
8 |
Correct |
7 ms |
7680 KB |
Output is correct |
9 |
Correct |
124 ms |
27512 KB |
Output is correct |
10 |
Correct |
148 ms |
31992 KB |
Output is correct |
11 |
Correct |
147 ms |
31608 KB |
Output is correct |
12 |
Correct |
160 ms |
33016 KB |
Output is correct |
13 |
Correct |
143 ms |
35356 KB |
Output is correct |
14 |
Correct |
125 ms |
27640 KB |
Output is correct |
15 |
Correct |
224 ms |
34052 KB |
Output is correct |
16 |
Correct |
231 ms |
33320 KB |
Output is correct |
17 |
Correct |
239 ms |
35040 KB |
Output is correct |
18 |
Correct |
221 ms |
37212 KB |
Output is correct |
19 |
Correct |
343 ms |
39216 KB |
Output is correct |
20 |
Correct |
339 ms |
40572 KB |
Output is correct |
21 |
Correct |
350 ms |
39824 KB |
Output is correct |
22 |
Correct |
360 ms |
40340 KB |
Output is correct |
23 |
Correct |
356 ms |
40180 KB |
Output is correct |
24 |
Correct |
347 ms |
39076 KB |
Output is correct |
25 |
Correct |
340 ms |
39876 KB |
Output is correct |
26 |
Correct |
335 ms |
38708 KB |
Output is correct |
27 |
Correct |
6 ms |
7680 KB |
Output is correct |
28 |
Correct |
6 ms |
7680 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7552 KB |
Output is correct |
31 |
Correct |
6 ms |
7680 KB |
Output is correct |
32 |
Correct |
7 ms |
7680 KB |
Output is correct |
33 |
Correct |
6 ms |
7680 KB |
Output is correct |
34 |
Correct |
6 ms |
7808 KB |
Output is correct |
35 |
Correct |
6 ms |
7680 KB |
Output is correct |
36 |
Correct |
20 ms |
10880 KB |
Output is correct |
37 |
Correct |
149 ms |
33016 KB |
Output is correct |
38 |
Correct |
154 ms |
33020 KB |
Output is correct |
39 |
Correct |
148 ms |
33016 KB |
Output is correct |
40 |
Correct |
147 ms |
32760 KB |
Output is correct |
41 |
Correct |
145 ms |
32632 KB |
Output is correct |
42 |
Correct |
138 ms |
30456 KB |
Output is correct |
43 |
Correct |
155 ms |
33144 KB |
Output is correct |
44 |
Correct |
150 ms |
33200 KB |
Output is correct |
45 |
Correct |
137 ms |
36120 KB |
Output is correct |
46 |
Correct |
156 ms |
33016 KB |
Output is correct |
47 |
Correct |
29 ms |
11000 KB |
Output is correct |
48 |
Correct |
258 ms |
35648 KB |
Output is correct |
49 |
Correct |
261 ms |
35680 KB |
Output is correct |
50 |
Correct |
261 ms |
35620 KB |
Output is correct |
51 |
Correct |
257 ms |
35436 KB |
Output is correct |
52 |
Correct |
249 ms |
33944 KB |
Output is correct |
53 |
Correct |
220 ms |
28016 KB |
Output is correct |
54 |
Correct |
278 ms |
36352 KB |
Output is correct |
55 |
Correct |
272 ms |
35676 KB |
Output is correct |
56 |
Correct |
340 ms |
38268 KB |
Output is correct |
57 |
Correct |
285 ms |
36316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7424 KB |
Output is correct |
5 |
Correct |
9 ms |
7552 KB |
Output is correct |
6 |
Correct |
6 ms |
7680 KB |
Output is correct |
7 |
Correct |
7 ms |
7632 KB |
Output is correct |
8 |
Correct |
7 ms |
7680 KB |
Output is correct |
9 |
Correct |
7 ms |
7680 KB |
Output is correct |
10 |
Correct |
124 ms |
27512 KB |
Output is correct |
11 |
Correct |
148 ms |
31992 KB |
Output is correct |
12 |
Correct |
147 ms |
31608 KB |
Output is correct |
13 |
Correct |
160 ms |
33016 KB |
Output is correct |
14 |
Correct |
143 ms |
35356 KB |
Output is correct |
15 |
Correct |
125 ms |
27640 KB |
Output is correct |
16 |
Correct |
224 ms |
34052 KB |
Output is correct |
17 |
Correct |
231 ms |
33320 KB |
Output is correct |
18 |
Correct |
239 ms |
35040 KB |
Output is correct |
19 |
Correct |
221 ms |
37212 KB |
Output is correct |
20 |
Correct |
343 ms |
39216 KB |
Output is correct |
21 |
Correct |
339 ms |
40572 KB |
Output is correct |
22 |
Correct |
350 ms |
39824 KB |
Output is correct |
23 |
Correct |
360 ms |
40340 KB |
Output is correct |
24 |
Correct |
356 ms |
40180 KB |
Output is correct |
25 |
Correct |
347 ms |
39076 KB |
Output is correct |
26 |
Correct |
340 ms |
39876 KB |
Output is correct |
27 |
Correct |
335 ms |
38708 KB |
Output is correct |
28 |
Correct |
6 ms |
7680 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7680 KB |
Output is correct |
31 |
Correct |
6 ms |
7552 KB |
Output is correct |
32 |
Correct |
6 ms |
7680 KB |
Output is correct |
33 |
Correct |
7 ms |
7680 KB |
Output is correct |
34 |
Correct |
6 ms |
7680 KB |
Output is correct |
35 |
Correct |
6 ms |
7808 KB |
Output is correct |
36 |
Correct |
6 ms |
7680 KB |
Output is correct |
37 |
Correct |
20 ms |
10880 KB |
Output is correct |
38 |
Correct |
149 ms |
33016 KB |
Output is correct |
39 |
Correct |
154 ms |
33020 KB |
Output is correct |
40 |
Correct |
148 ms |
33016 KB |
Output is correct |
41 |
Correct |
147 ms |
32760 KB |
Output is correct |
42 |
Correct |
145 ms |
32632 KB |
Output is correct |
43 |
Correct |
138 ms |
30456 KB |
Output is correct |
44 |
Correct |
155 ms |
33144 KB |
Output is correct |
45 |
Correct |
150 ms |
33200 KB |
Output is correct |
46 |
Correct |
137 ms |
36120 KB |
Output is correct |
47 |
Correct |
156 ms |
33016 KB |
Output is correct |
48 |
Correct |
29 ms |
11000 KB |
Output is correct |
49 |
Correct |
258 ms |
35648 KB |
Output is correct |
50 |
Correct |
261 ms |
35680 KB |
Output is correct |
51 |
Correct |
261 ms |
35620 KB |
Output is correct |
52 |
Correct |
257 ms |
35436 KB |
Output is correct |
53 |
Correct |
249 ms |
33944 KB |
Output is correct |
54 |
Correct |
220 ms |
28016 KB |
Output is correct |
55 |
Correct |
278 ms |
36352 KB |
Output is correct |
56 |
Correct |
272 ms |
35676 KB |
Output is correct |
57 |
Correct |
340 ms |
38268 KB |
Output is correct |
58 |
Correct |
285 ms |
36316 KB |
Output is correct |
59 |
Correct |
111 ms |
14968 KB |
Output is correct |
60 |
Correct |
261 ms |
35352 KB |
Output is correct |
61 |
Correct |
260 ms |
34608 KB |
Output is correct |
62 |
Correct |
277 ms |
36320 KB |
Output is correct |
63 |
Correct |
386 ms |
38624 KB |
Output is correct |
64 |
Correct |
6 ms |
7680 KB |
Output is correct |
65 |
Correct |
6 ms |
7680 KB |
Output is correct |
66 |
Correct |
7 ms |
7680 KB |
Output is correct |
67 |
Correct |
7 ms |
7808 KB |
Output is correct |
68 |
Correct |
6 ms |
7552 KB |
Output is correct |
69 |
Correct |
8 ms |
7808 KB |
Output is correct |
70 |
Correct |
7 ms |
7808 KB |
Output is correct |
71 |
Correct |
7 ms |
7808 KB |
Output is correct |
72 |
Correct |
6 ms |
7680 KB |
Output is correct |
73 |
Correct |
7 ms |
7808 KB |
Output is correct |
74 |
Correct |
215 ms |
43000 KB |
Output is correct |
75 |
Correct |
146 ms |
33016 KB |
Output is correct |
76 |
Correct |
156 ms |
33144 KB |
Output is correct |
77 |
Correct |
170 ms |
36472 KB |
Output is correct |
78 |
Correct |
160 ms |
40568 KB |
Output is correct |
79 |
Correct |
131 ms |
33144 KB |
Output is correct |
80 |
Correct |
180 ms |
38256 KB |
Output is correct |
81 |
Correct |
233 ms |
45944 KB |
Output is correct |
82 |
Correct |
271 ms |
48504 KB |
Output is correct |
83 |
Correct |
280 ms |
54008 KB |
Output is correct |
84 |
Correct |
140 ms |
35960 KB |
Output is correct |
85 |
Correct |
256 ms |
49528 KB |
Output is correct |
86 |
Correct |
112 ms |
20396 KB |
Output is correct |
87 |
Correct |
269 ms |
35616 KB |
Output is correct |
88 |
Correct |
267 ms |
35680 KB |
Output is correct |
89 |
Correct |
367 ms |
37652 KB |
Output is correct |
90 |
Correct |
298 ms |
44656 KB |
Output is correct |
91 |
Correct |
309 ms |
41760 KB |
Output is correct |
92 |
Correct |
393 ms |
39892 KB |
Output is correct |
93 |
Correct |
385 ms |
47792 KB |
Output is correct |
94 |
Correct |
482 ms |
50648 KB |
Output is correct |
95 |
Correct |
424 ms |
55592 KB |
Output is correct |
96 |
Correct |
341 ms |
38308 KB |
Output is correct |
97 |
Correct |
437 ms |
44476 KB |
Output is correct |