#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int N = 3e5;
const int LOG = 19;
int par[N+2], val[N+2], deg[N+2], dep[N+2];
int anc[N+2][LOG+2];
int find(int x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
for (int i = 0; i < N+M; i++) {
par[i] = i;
anc[i][0] = i;
val[i] = -1;
}
vector<iii> edge;
for (int i = 0; i < M; i++) {
edge.push_back({W[i],{U[i],V[i]}});
}
sort(edge.begin(),edge.end());
for (int i = 0; i < M; i++) {
int u = edge[i].se.fi;
int v = edge[i].se.se;
int w = edge[i].fi;
int pu = find(u);
int pv = find(v);
deg[u]++, deg[v]++;
par[pu] = par[pv] = N+i;
anc[pu][0] = anc[pv][0] = N+i;
val[N+i] = -1;
if (deg[u] >= 3 || deg[v] >= 3 || pu == pv || val[pu] != -1 || val[pv] != -1) val[N+i] = w;
}
for (int i = 1; i <= LOG; i++) {
for (int j = 0; j < N+M; j++) {
anc[j][i] = anc[anc[j][i-1]][i-1];
}
}
for (int i = N+M-2; i >= 0; i--) {
dep[i] = dep[anc[i][0]]+1;
}
}
int lca(int X, int Y) {
if (dep[X] < dep[Y]) swap(X,Y);
for (int i = LOG; i >= 0; i--) {
if (dep[X]-(1<<i) >= dep[Y]) {
X = anc[X][i];
}
}
if (X == Y) return X;
for (int i = LOG; i >= 0; i--) {
if (anc[X][i] != anc[Y][i]) {
X = anc[X][i];
Y = anc[Y][i];
}
}
return anc[X][0];
}
int getMinimumFuelCapacity(int X, int Y) {
if (find(X) != find(Y)) return -1;
int z = lca(X,Y);
// cout << ">> " << z << '\n';
if (val[z] != -1) return val[z];
for (int i = LOG; i >= 0; i--) {
if (dep[z] < (1<<i)) continue;
if (val[anc[z][i]] == -1) z = anc[z][i];
}
return val[anc[z][0]];
}
/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
692 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
77 ms |
19896 KB |
Output is correct |
10 |
Correct |
92 ms |
24224 KB |
Output is correct |
11 |
Correct |
93 ms |
23788 KB |
Output is correct |
12 |
Correct |
101 ms |
25256 KB |
Output is correct |
13 |
Correct |
110 ms |
25912 KB |
Output is correct |
14 |
Correct |
114 ms |
20132 KB |
Output is correct |
15 |
Correct |
251 ms |
28304 KB |
Output is correct |
16 |
Correct |
269 ms |
27584 KB |
Output is correct |
17 |
Correct |
257 ms |
29080 KB |
Output is correct |
18 |
Correct |
382 ms |
29752 KB |
Output is correct |
19 |
Correct |
100 ms |
9284 KB |
Output is correct |
20 |
Correct |
261 ms |
28372 KB |
Output is correct |
21 |
Correct |
266 ms |
27852 KB |
Output is correct |
22 |
Correct |
265 ms |
29424 KB |
Output is correct |
23 |
Correct |
406 ms |
30092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
327 ms |
28084 KB |
Output is correct |
4 |
Correct |
327 ms |
29252 KB |
Output is correct |
5 |
Correct |
320 ms |
28688 KB |
Output is correct |
6 |
Correct |
367 ms |
29276 KB |
Output is correct |
7 |
Correct |
342 ms |
29048 KB |
Output is correct |
8 |
Correct |
309 ms |
27924 KB |
Output is correct |
9 |
Correct |
328 ms |
28736 KB |
Output is correct |
10 |
Correct |
339 ms |
27752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
692 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
568 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
692 KB |
Output is correct |
25 |
Correct |
2 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
692 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
77 ms |
19896 KB |
Output is correct |
11 |
Correct |
92 ms |
24224 KB |
Output is correct |
12 |
Correct |
93 ms |
23788 KB |
Output is correct |
13 |
Correct |
101 ms |
25256 KB |
Output is correct |
14 |
Correct |
110 ms |
25912 KB |
Output is correct |
15 |
Correct |
2 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
568 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
692 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
716 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
716 KB |
Output is correct |
34 |
Correct |
10 ms |
3804 KB |
Output is correct |
35 |
Correct |
100 ms |
24872 KB |
Output is correct |
36 |
Correct |
98 ms |
24888 KB |
Output is correct |
37 |
Correct |
100 ms |
24876 KB |
Output is correct |
38 |
Correct |
96 ms |
24608 KB |
Output is correct |
39 |
Correct |
99 ms |
24376 KB |
Output is correct |
40 |
Correct |
91 ms |
22456 KB |
Output is correct |
41 |
Correct |
101 ms |
25348 KB |
Output is correct |
42 |
Correct |
99 ms |
24888 KB |
Output is correct |
43 |
Correct |
108 ms |
24868 KB |
Output is correct |
44 |
Correct |
103 ms |
25260 KB |
Output is correct |
45 |
Correct |
147 ms |
30264 KB |
Output is correct |
46 |
Correct |
101 ms |
24868 KB |
Output is correct |
47 |
Correct |
101 ms |
24864 KB |
Output is correct |
48 |
Correct |
115 ms |
26388 KB |
Output is correct |
49 |
Correct |
118 ms |
24268 KB |
Output is correct |
50 |
Correct |
94 ms |
18968 KB |
Output is correct |
51 |
Correct |
127 ms |
25848 KB |
Output is correct |
52 |
Correct |
156 ms |
34408 KB |
Output is correct |
53 |
Correct |
174 ms |
37004 KB |
Output is correct |
54 |
Correct |
189 ms |
39980 KB |
Output is correct |
55 |
Correct |
107 ms |
24888 KB |
Output is correct |
56 |
Correct |
174 ms |
35992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
692 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
77 ms |
19896 KB |
Output is correct |
10 |
Correct |
92 ms |
24224 KB |
Output is correct |
11 |
Correct |
93 ms |
23788 KB |
Output is correct |
12 |
Correct |
101 ms |
25256 KB |
Output is correct |
13 |
Correct |
110 ms |
25912 KB |
Output is correct |
14 |
Correct |
114 ms |
20132 KB |
Output is correct |
15 |
Correct |
251 ms |
28304 KB |
Output is correct |
16 |
Correct |
269 ms |
27584 KB |
Output is correct |
17 |
Correct |
257 ms |
29080 KB |
Output is correct |
18 |
Correct |
382 ms |
29752 KB |
Output is correct |
19 |
Correct |
327 ms |
28084 KB |
Output is correct |
20 |
Correct |
327 ms |
29252 KB |
Output is correct |
21 |
Correct |
320 ms |
28688 KB |
Output is correct |
22 |
Correct |
367 ms |
29276 KB |
Output is correct |
23 |
Correct |
342 ms |
29048 KB |
Output is correct |
24 |
Correct |
309 ms |
27924 KB |
Output is correct |
25 |
Correct |
328 ms |
28736 KB |
Output is correct |
26 |
Correct |
339 ms |
27752 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
10 ms |
3804 KB |
Output is correct |
37 |
Correct |
100 ms |
24872 KB |
Output is correct |
38 |
Correct |
98 ms |
24888 KB |
Output is correct |
39 |
Correct |
100 ms |
24876 KB |
Output is correct |
40 |
Correct |
96 ms |
24608 KB |
Output is correct |
41 |
Correct |
99 ms |
24376 KB |
Output is correct |
42 |
Correct |
91 ms |
22456 KB |
Output is correct |
43 |
Correct |
101 ms |
25348 KB |
Output is correct |
44 |
Correct |
99 ms |
24888 KB |
Output is correct |
45 |
Correct |
108 ms |
24868 KB |
Output is correct |
46 |
Correct |
103 ms |
25260 KB |
Output is correct |
47 |
Correct |
22 ms |
3852 KB |
Output is correct |
48 |
Correct |
246 ms |
28848 KB |
Output is correct |
49 |
Correct |
242 ms |
28856 KB |
Output is correct |
50 |
Correct |
257 ms |
28708 KB |
Output is correct |
51 |
Correct |
253 ms |
28636 KB |
Output is correct |
52 |
Correct |
240 ms |
27124 KB |
Output is correct |
53 |
Correct |
208 ms |
21812 KB |
Output is correct |
54 |
Correct |
248 ms |
29432 KB |
Output is correct |
55 |
Correct |
258 ms |
28824 KB |
Output is correct |
56 |
Correct |
383 ms |
28908 KB |
Output is correct |
57 |
Correct |
249 ms |
29360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
432 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
692 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
77 ms |
19896 KB |
Output is correct |
11 |
Correct |
92 ms |
24224 KB |
Output is correct |
12 |
Correct |
93 ms |
23788 KB |
Output is correct |
13 |
Correct |
101 ms |
25256 KB |
Output is correct |
14 |
Correct |
110 ms |
25912 KB |
Output is correct |
15 |
Correct |
114 ms |
20132 KB |
Output is correct |
16 |
Correct |
251 ms |
28304 KB |
Output is correct |
17 |
Correct |
269 ms |
27584 KB |
Output is correct |
18 |
Correct |
257 ms |
29080 KB |
Output is correct |
19 |
Correct |
382 ms |
29752 KB |
Output is correct |
20 |
Correct |
327 ms |
28084 KB |
Output is correct |
21 |
Correct |
327 ms |
29252 KB |
Output is correct |
22 |
Correct |
320 ms |
28688 KB |
Output is correct |
23 |
Correct |
367 ms |
29276 KB |
Output is correct |
24 |
Correct |
342 ms |
29048 KB |
Output is correct |
25 |
Correct |
309 ms |
27924 KB |
Output is correct |
26 |
Correct |
328 ms |
28736 KB |
Output is correct |
27 |
Correct |
339 ms |
27752 KB |
Output is correct |
28 |
Correct |
2 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
568 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
588 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
10 ms |
3804 KB |
Output is correct |
38 |
Correct |
100 ms |
24872 KB |
Output is correct |
39 |
Correct |
98 ms |
24888 KB |
Output is correct |
40 |
Correct |
100 ms |
24876 KB |
Output is correct |
41 |
Correct |
96 ms |
24608 KB |
Output is correct |
42 |
Correct |
99 ms |
24376 KB |
Output is correct |
43 |
Correct |
91 ms |
22456 KB |
Output is correct |
44 |
Correct |
101 ms |
25348 KB |
Output is correct |
45 |
Correct |
99 ms |
24888 KB |
Output is correct |
46 |
Correct |
108 ms |
24868 KB |
Output is correct |
47 |
Correct |
103 ms |
25260 KB |
Output is correct |
48 |
Correct |
22 ms |
3852 KB |
Output is correct |
49 |
Correct |
246 ms |
28848 KB |
Output is correct |
50 |
Correct |
242 ms |
28856 KB |
Output is correct |
51 |
Correct |
257 ms |
28708 KB |
Output is correct |
52 |
Correct |
253 ms |
28636 KB |
Output is correct |
53 |
Correct |
240 ms |
27124 KB |
Output is correct |
54 |
Correct |
208 ms |
21812 KB |
Output is correct |
55 |
Correct |
248 ms |
29432 KB |
Output is correct |
56 |
Correct |
258 ms |
28824 KB |
Output is correct |
57 |
Correct |
383 ms |
28908 KB |
Output is correct |
58 |
Correct |
249 ms |
29360 KB |
Output is correct |
59 |
Correct |
100 ms |
9284 KB |
Output is correct |
60 |
Correct |
261 ms |
28372 KB |
Output is correct |
61 |
Correct |
266 ms |
27852 KB |
Output is correct |
62 |
Correct |
265 ms |
29424 KB |
Output is correct |
63 |
Correct |
406 ms |
30092 KB |
Output is correct |
64 |
Correct |
1 ms |
460 KB |
Output is correct |
65 |
Correct |
2 ms |
460 KB |
Output is correct |
66 |
Correct |
1 ms |
588 KB |
Output is correct |
67 |
Correct |
2 ms |
588 KB |
Output is correct |
68 |
Correct |
1 ms |
460 KB |
Output is correct |
69 |
Correct |
2 ms |
692 KB |
Output is correct |
70 |
Correct |
2 ms |
716 KB |
Output is correct |
71 |
Correct |
2 ms |
716 KB |
Output is correct |
72 |
Correct |
1 ms |
460 KB |
Output is correct |
73 |
Correct |
2 ms |
716 KB |
Output is correct |
74 |
Correct |
147 ms |
30264 KB |
Output is correct |
75 |
Correct |
101 ms |
24868 KB |
Output is correct |
76 |
Correct |
101 ms |
24864 KB |
Output is correct |
77 |
Correct |
115 ms |
26388 KB |
Output is correct |
78 |
Correct |
118 ms |
24268 KB |
Output is correct |
79 |
Correct |
94 ms |
18968 KB |
Output is correct |
80 |
Correct |
127 ms |
25848 KB |
Output is correct |
81 |
Correct |
156 ms |
34408 KB |
Output is correct |
82 |
Correct |
174 ms |
37004 KB |
Output is correct |
83 |
Correct |
189 ms |
39980 KB |
Output is correct |
84 |
Correct |
107 ms |
24888 KB |
Output is correct |
85 |
Correct |
174 ms |
35992 KB |
Output is correct |
86 |
Correct |
85 ms |
11872 KB |
Output is correct |
87 |
Correct |
236 ms |
28832 KB |
Output is correct |
88 |
Correct |
277 ms |
28728 KB |
Output is correct |
89 |
Correct |
308 ms |
28776 KB |
Output is correct |
90 |
Correct |
227 ms |
28852 KB |
Output is correct |
91 |
Correct |
237 ms |
27752 KB |
Output is correct |
92 |
Correct |
341 ms |
28836 KB |
Output is correct |
93 |
Correct |
301 ms |
37816 KB |
Output is correct |
94 |
Correct |
413 ms |
40644 KB |
Output is correct |
95 |
Correct |
361 ms |
43848 KB |
Output is correct |
96 |
Correct |
368 ms |
29056 KB |
Output is correct |
97 |
Correct |
357 ms |
34640 KB |
Output is correct |