#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 2e5 + 5;
int n, m, fa[N + M], val[N + M], col[N + M], deg[N];
int par[19][N + M], nxt[N + M], d[N + M];
vector<int> adj[N + M];
inline int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }
inline void merge(int u, int v, int w) {
deg[u] ++, deg[v] ++;
bool ok = deg[u] >= 3 || deg[v] >= 3;
if ((u = find(u)) == (v = find(v))) {
if (!col[u])
col[u] = 1, val[u] = w;
return ;
}
fa[u] = fa[v] = ++ n;
adj[n].push_back(u);
adj[n].push_back(v);
col[n] = ok | col[u] | col[v], val[n] = w;
}
void pre_dfs(int u, int f = 0) {
if (col[u]) f = u;
nxt[u] = f;
for (int v : adj[u]) {
d[v] = d[par[0][v] = u] + 1;
for (int i = 1; i < 19; i++)
par[i][v] = par[i - 1][par[i - 1][v]];
pre_dfs(v, f);
}
}
inline int lca(int u, int v) {
if (d[u] > d[v]) swap(u, v);
for (int i = 19; i --; )
if ((d[v] - d[u]) >> i & 1)
v = par[i][v];
if (u == v) return u;
for (int i = 19; i --; )
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
return par[0][u];
}
void init(int _n, int _m, vector<int> U, vector<int> V, vector<int> W) {
n = _n, m = _m;
iota(fa, fa + N + M, 0);
vector<int> ids(m);
iota(ids.begin(), ids.end(), 0);
sort(ids.begin(), ids.end(), [&](const int &i, const int &j){
return W[i] < W[j];
});
for (int i : ids)
merge(U[i] + 1, V[i] + 1, W[i]);
val[0] = -1, pre_dfs(n);
}
int getMinimumFuelCapacity(int X, int Y) {
return val[nxt[lca(X + 1, Y + 1)]];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8660 KB |
Output is correct |
2 |
Correct |
4 ms |
8660 KB |
Output is correct |
3 |
Correct |
4 ms |
8660 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
5 ms |
8788 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
6 ms |
8820 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
98 ms |
27052 KB |
Output is correct |
10 |
Correct |
125 ms |
31140 KB |
Output is correct |
11 |
Correct |
121 ms |
30740 KB |
Output is correct |
12 |
Correct |
135 ms |
32120 KB |
Output is correct |
13 |
Correct |
106 ms |
36004 KB |
Output is correct |
14 |
Correct |
112 ms |
27184 KB |
Output is correct |
15 |
Correct |
264 ms |
32836 KB |
Output is correct |
16 |
Correct |
243 ms |
32140 KB |
Output is correct |
17 |
Correct |
275 ms |
33648 KB |
Output is correct |
18 |
Correct |
330 ms |
37580 KB |
Output is correct |
19 |
Correct |
88 ms |
15816 KB |
Output is correct |
20 |
Correct |
244 ms |
33960 KB |
Output is correct |
21 |
Correct |
260 ms |
33376 KB |
Output is correct |
22 |
Correct |
259 ms |
34836 KB |
Output is correct |
23 |
Correct |
341 ms |
38740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8660 KB |
Output is correct |
2 |
Correct |
4 ms |
8660 KB |
Output is correct |
3 |
Correct |
299 ms |
40888 KB |
Output is correct |
4 |
Correct |
307 ms |
42324 KB |
Output is correct |
5 |
Correct |
332 ms |
41572 KB |
Output is correct |
6 |
Correct |
291 ms |
42164 KB |
Output is correct |
7 |
Correct |
327 ms |
41928 KB |
Output is correct |
8 |
Correct |
292 ms |
40652 KB |
Output is correct |
9 |
Correct |
325 ms |
41672 KB |
Output is correct |
10 |
Correct |
273 ms |
40384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8660 KB |
Output is correct |
2 |
Correct |
4 ms |
8660 KB |
Output is correct |
3 |
Correct |
4 ms |
8660 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
5 ms |
8788 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
6 ms |
8820 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8660 KB |
Output is correct |
10 |
Correct |
6 ms |
8856 KB |
Output is correct |
11 |
Correct |
6 ms |
8788 KB |
Output is correct |
12 |
Correct |
6 ms |
8788 KB |
Output is correct |
13 |
Correct |
5 ms |
8788 KB |
Output is correct |
14 |
Correct |
5 ms |
8788 KB |
Output is correct |
15 |
Correct |
5 ms |
8788 KB |
Output is correct |
16 |
Correct |
5 ms |
8788 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
5 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
8756 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
8788 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
5 ms |
8812 KB |
Output is correct |
24 |
Correct |
7 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8916 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
7 ms |
8940 KB |
Output is correct |
28 |
Correct |
7 ms |
8896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8660 KB |
Output is correct |
2 |
Correct |
4 ms |
8660 KB |
Output is correct |
3 |
Correct |
4 ms |
8660 KB |
Output is correct |
4 |
Correct |
4 ms |
8660 KB |
Output is correct |
5 |
Correct |
5 ms |
8660 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
5 ms |
8788 KB |
Output is correct |
8 |
Correct |
6 ms |
8820 KB |
Output is correct |
9 |
Correct |
6 ms |
8916 KB |
Output is correct |
10 |
Correct |
98 ms |
27052 KB |
Output is correct |
11 |
Correct |
125 ms |
31140 KB |
Output is correct |
12 |
Correct |
121 ms |
30740 KB |
Output is correct |
13 |
Correct |
135 ms |
32120 KB |
Output is correct |
14 |
Correct |
106 ms |
36004 KB |
Output is correct |
15 |
Correct |
6 ms |
8856 KB |
Output is correct |
16 |
Correct |
6 ms |
8788 KB |
Output is correct |
17 |
Correct |
6 ms |
8788 KB |
Output is correct |
18 |
Correct |
5 ms |
8788 KB |
Output is correct |
19 |
Correct |
5 ms |
8788 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
21 |
Correct |
5 ms |
8788 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8788 KB |
Output is correct |
24 |
Correct |
5 ms |
8756 KB |
Output is correct |
25 |
Correct |
5 ms |
8788 KB |
Output is correct |
26 |
Correct |
5 ms |
8788 KB |
Output is correct |
27 |
Correct |
5 ms |
8660 KB |
Output is correct |
28 |
Correct |
5 ms |
8812 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
6 ms |
8916 KB |
Output is correct |
32 |
Correct |
7 ms |
8940 KB |
Output is correct |
33 |
Correct |
7 ms |
8896 KB |
Output is correct |
34 |
Correct |
15 ms |
11852 KB |
Output is correct |
35 |
Correct |
135 ms |
32072 KB |
Output is correct |
36 |
Correct |
124 ms |
32072 KB |
Output is correct |
37 |
Correct |
125 ms |
32140 KB |
Output is correct |
38 |
Correct |
120 ms |
31688 KB |
Output is correct |
39 |
Correct |
131 ms |
31668 KB |
Output is correct |
40 |
Correct |
113 ms |
29800 KB |
Output is correct |
41 |
Correct |
124 ms |
32120 KB |
Output is correct |
42 |
Correct |
126 ms |
32092 KB |
Output is correct |
43 |
Correct |
119 ms |
37264 KB |
Output is correct |
44 |
Correct |
129 ms |
32200 KB |
Output is correct |
45 |
Correct |
124 ms |
30916 KB |
Output is correct |
46 |
Correct |
132 ms |
32072 KB |
Output is correct |
47 |
Correct |
132 ms |
32072 KB |
Output is correct |
48 |
Correct |
125 ms |
33884 KB |
Output is correct |
49 |
Correct |
63 ms |
13172 KB |
Output is correct |
50 |
Correct |
54 ms |
13632 KB |
Output is correct |
51 |
Correct |
145 ms |
26900 KB |
Output is correct |
52 |
Correct |
162 ms |
33820 KB |
Output is correct |
53 |
Correct |
158 ms |
35288 KB |
Output is correct |
54 |
Correct |
172 ms |
34800 KB |
Output is correct |
55 |
Correct |
97 ms |
36736 KB |
Output is correct |
56 |
Correct |
175 ms |
36484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8660 KB |
Output is correct |
2 |
Correct |
4 ms |
8660 KB |
Output is correct |
3 |
Correct |
4 ms |
8660 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
5 ms |
8788 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
6 ms |
8820 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
98 ms |
27052 KB |
Output is correct |
10 |
Correct |
125 ms |
31140 KB |
Output is correct |
11 |
Correct |
121 ms |
30740 KB |
Output is correct |
12 |
Correct |
135 ms |
32120 KB |
Output is correct |
13 |
Correct |
106 ms |
36004 KB |
Output is correct |
14 |
Correct |
112 ms |
27184 KB |
Output is correct |
15 |
Correct |
264 ms |
32836 KB |
Output is correct |
16 |
Correct |
243 ms |
32140 KB |
Output is correct |
17 |
Correct |
275 ms |
33648 KB |
Output is correct |
18 |
Correct |
330 ms |
37580 KB |
Output is correct |
19 |
Correct |
299 ms |
40888 KB |
Output is correct |
20 |
Correct |
307 ms |
42324 KB |
Output is correct |
21 |
Correct |
332 ms |
41572 KB |
Output is correct |
22 |
Correct |
291 ms |
42164 KB |
Output is correct |
23 |
Correct |
327 ms |
41928 KB |
Output is correct |
24 |
Correct |
292 ms |
40652 KB |
Output is correct |
25 |
Correct |
325 ms |
41672 KB |
Output is correct |
26 |
Correct |
273 ms |
40384 KB |
Output is correct |
27 |
Correct |
6 ms |
8856 KB |
Output is correct |
28 |
Correct |
6 ms |
8788 KB |
Output is correct |
29 |
Correct |
6 ms |
8788 KB |
Output is correct |
30 |
Correct |
5 ms |
8788 KB |
Output is correct |
31 |
Correct |
5 ms |
8788 KB |
Output is correct |
32 |
Correct |
5 ms |
8788 KB |
Output is correct |
33 |
Correct |
5 ms |
8788 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8788 KB |
Output is correct |
36 |
Correct |
15 ms |
11852 KB |
Output is correct |
37 |
Correct |
135 ms |
32072 KB |
Output is correct |
38 |
Correct |
124 ms |
32072 KB |
Output is correct |
39 |
Correct |
125 ms |
32140 KB |
Output is correct |
40 |
Correct |
120 ms |
31688 KB |
Output is correct |
41 |
Correct |
131 ms |
31668 KB |
Output is correct |
42 |
Correct |
113 ms |
29800 KB |
Output is correct |
43 |
Correct |
124 ms |
32120 KB |
Output is correct |
44 |
Correct |
126 ms |
32092 KB |
Output is correct |
45 |
Correct |
119 ms |
37264 KB |
Output is correct |
46 |
Correct |
129 ms |
32200 KB |
Output is correct |
47 |
Correct |
30 ms |
11832 KB |
Output is correct |
48 |
Correct |
257 ms |
34212 KB |
Output is correct |
49 |
Correct |
265 ms |
34324 KB |
Output is correct |
50 |
Correct |
261 ms |
34132 KB |
Output is correct |
51 |
Correct |
244 ms |
34124 KB |
Output is correct |
52 |
Correct |
225 ms |
32860 KB |
Output is correct |
53 |
Correct |
199 ms |
27488 KB |
Output is correct |
54 |
Correct |
258 ms |
34856 KB |
Output is correct |
55 |
Correct |
289 ms |
34288 KB |
Output is correct |
56 |
Correct |
331 ms |
38264 KB |
Output is correct |
57 |
Correct |
322 ms |
35000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8660 KB |
Output is correct |
2 |
Correct |
4 ms |
8660 KB |
Output is correct |
3 |
Correct |
4 ms |
8660 KB |
Output is correct |
4 |
Correct |
4 ms |
8660 KB |
Output is correct |
5 |
Correct |
5 ms |
8660 KB |
Output is correct |
6 |
Correct |
5 ms |
8788 KB |
Output is correct |
7 |
Correct |
5 ms |
8788 KB |
Output is correct |
8 |
Correct |
6 ms |
8820 KB |
Output is correct |
9 |
Correct |
6 ms |
8916 KB |
Output is correct |
10 |
Correct |
98 ms |
27052 KB |
Output is correct |
11 |
Correct |
125 ms |
31140 KB |
Output is correct |
12 |
Correct |
121 ms |
30740 KB |
Output is correct |
13 |
Correct |
135 ms |
32120 KB |
Output is correct |
14 |
Correct |
106 ms |
36004 KB |
Output is correct |
15 |
Correct |
112 ms |
27184 KB |
Output is correct |
16 |
Correct |
264 ms |
32836 KB |
Output is correct |
17 |
Correct |
243 ms |
32140 KB |
Output is correct |
18 |
Correct |
275 ms |
33648 KB |
Output is correct |
19 |
Correct |
330 ms |
37580 KB |
Output is correct |
20 |
Correct |
299 ms |
40888 KB |
Output is correct |
21 |
Correct |
307 ms |
42324 KB |
Output is correct |
22 |
Correct |
332 ms |
41572 KB |
Output is correct |
23 |
Correct |
291 ms |
42164 KB |
Output is correct |
24 |
Correct |
327 ms |
41928 KB |
Output is correct |
25 |
Correct |
292 ms |
40652 KB |
Output is correct |
26 |
Correct |
325 ms |
41672 KB |
Output is correct |
27 |
Correct |
273 ms |
40384 KB |
Output is correct |
28 |
Correct |
6 ms |
8856 KB |
Output is correct |
29 |
Correct |
6 ms |
8788 KB |
Output is correct |
30 |
Correct |
6 ms |
8788 KB |
Output is correct |
31 |
Correct |
5 ms |
8788 KB |
Output is correct |
32 |
Correct |
5 ms |
8788 KB |
Output is correct |
33 |
Correct |
5 ms |
8788 KB |
Output is correct |
34 |
Correct |
5 ms |
8788 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
5 ms |
8788 KB |
Output is correct |
37 |
Correct |
15 ms |
11852 KB |
Output is correct |
38 |
Correct |
135 ms |
32072 KB |
Output is correct |
39 |
Correct |
124 ms |
32072 KB |
Output is correct |
40 |
Correct |
125 ms |
32140 KB |
Output is correct |
41 |
Correct |
120 ms |
31688 KB |
Output is correct |
42 |
Correct |
131 ms |
31668 KB |
Output is correct |
43 |
Correct |
113 ms |
29800 KB |
Output is correct |
44 |
Correct |
124 ms |
32120 KB |
Output is correct |
45 |
Correct |
126 ms |
32092 KB |
Output is correct |
46 |
Correct |
119 ms |
37264 KB |
Output is correct |
47 |
Correct |
129 ms |
32200 KB |
Output is correct |
48 |
Correct |
30 ms |
11832 KB |
Output is correct |
49 |
Correct |
257 ms |
34212 KB |
Output is correct |
50 |
Correct |
265 ms |
34324 KB |
Output is correct |
51 |
Correct |
261 ms |
34132 KB |
Output is correct |
52 |
Correct |
244 ms |
34124 KB |
Output is correct |
53 |
Correct |
225 ms |
32860 KB |
Output is correct |
54 |
Correct |
199 ms |
27488 KB |
Output is correct |
55 |
Correct |
258 ms |
34856 KB |
Output is correct |
56 |
Correct |
289 ms |
34288 KB |
Output is correct |
57 |
Correct |
331 ms |
38264 KB |
Output is correct |
58 |
Correct |
322 ms |
35000 KB |
Output is correct |
59 |
Correct |
88 ms |
15816 KB |
Output is correct |
60 |
Correct |
244 ms |
33960 KB |
Output is correct |
61 |
Correct |
260 ms |
33376 KB |
Output is correct |
62 |
Correct |
259 ms |
34836 KB |
Output is correct |
63 |
Correct |
341 ms |
38740 KB |
Output is correct |
64 |
Correct |
5 ms |
8756 KB |
Output is correct |
65 |
Correct |
5 ms |
8788 KB |
Output is correct |
66 |
Correct |
5 ms |
8788 KB |
Output is correct |
67 |
Correct |
5 ms |
8660 KB |
Output is correct |
68 |
Correct |
5 ms |
8812 KB |
Output is correct |
69 |
Correct |
7 ms |
8916 KB |
Output is correct |
70 |
Correct |
5 ms |
8916 KB |
Output is correct |
71 |
Correct |
6 ms |
8916 KB |
Output is correct |
72 |
Correct |
7 ms |
8940 KB |
Output is correct |
73 |
Correct |
7 ms |
8896 KB |
Output is correct |
74 |
Correct |
124 ms |
30916 KB |
Output is correct |
75 |
Correct |
132 ms |
32072 KB |
Output is correct |
76 |
Correct |
132 ms |
32072 KB |
Output is correct |
77 |
Correct |
125 ms |
33884 KB |
Output is correct |
78 |
Correct |
63 ms |
13172 KB |
Output is correct |
79 |
Correct |
54 ms |
13632 KB |
Output is correct |
80 |
Correct |
145 ms |
26900 KB |
Output is correct |
81 |
Correct |
162 ms |
33820 KB |
Output is correct |
82 |
Correct |
158 ms |
35288 KB |
Output is correct |
83 |
Correct |
172 ms |
34800 KB |
Output is correct |
84 |
Correct |
97 ms |
36736 KB |
Output is correct |
85 |
Correct |
175 ms |
36484 KB |
Output is correct |
86 |
Correct |
68 ms |
17600 KB |
Output is correct |
87 |
Correct |
261 ms |
34296 KB |
Output is correct |
88 |
Correct |
263 ms |
34200 KB |
Output is correct |
89 |
Correct |
280 ms |
34452 KB |
Output is correct |
90 |
Correct |
172 ms |
15268 KB |
Output is correct |
91 |
Correct |
173 ms |
17292 KB |
Output is correct |
92 |
Correct |
250 ms |
29288 KB |
Output is correct |
93 |
Correct |
292 ms |
35536 KB |
Output is correct |
94 |
Correct |
320 ms |
36520 KB |
Output is correct |
95 |
Correct |
324 ms |
36448 KB |
Output is correct |
96 |
Correct |
339 ms |
38632 KB |
Output is correct |
97 |
Correct |
315 ms |
37336 KB |
Output is correct |