#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 15, M = 2e5 + 5, L = 21, INF = 0x3f3f3f3f;
int p[N], dpr[N], pr[N][L], ext[N], P[N], h[N], rt, D[N], deg[N];
vector<int> adj[N], Wtmp;
void smin(int &a, int b)
{
a = min(a, b);
}
void dfs(int v) {
for (int i = 1; i < L; i++) pr[v][i] = pr[pr[v][i - 1]][i - 1];
for (auto u: adj[v]) {
pr[u][0] = v;
h[u] = h[v] + 1;
dfs(u);
smin(ext[v], ext[u]);
}
}
int gpr(int u)
{
return dpr[u] == u ? u : dpr[u] = gpr(dpr[u]);
}
bool merge(int u, int v, int id) {
int tu = u, tv = v;
deg[tu]++, deg[tv]++;
u = gpr(u), v = gpr(v);
if (u == v) return false;
dpr[u] = dpr[v] = id + M;
D[id + M] = max({D[u], D[v], deg[tu], deg[tv]});
adj[id + M].push_back(u);
adj[id + M].push_back(v);
return true;
}
void init(int n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
iota(p, p + m, 0);
sort(p, p + m, [&](const int x, const int y) {return W[x] < W[y];});
iota(dpr, dpr + N, 0);
memset(ext, 0x3f, sizeof ext);
Wtmp = W;
for (int i = 0; i < m; i++) {
const int id = p[i];
if (!merge(U[id], V[id], id)) {
smin(ext[U[id]], W[id]);
smin(ext[V[id]], W[id]);
}
}
rt = gpr(0), pr[rt][0] = rt, dfs(rt);
}
int LCA(int X, int Y) {
for (int i = L - 1; i >= 0; i--) if (pr[Y][i] != pr[X][i]) Y = pr[Y][i], X = pr[X][i];
return pr[X][0];
}
int getMinimumFuelCapacity(int X, int Y) {
if (h[X] < h[Y]) swap(X, Y);
while (h[Y] < h[X]) X = pr[X][__lg(h[X] - h[Y])];
int w = INF, lca = LCA(X, Y);
for (int i = L - 1; i >= 0; i--) if (pr[Y][i] != pr[X][i] || D[pr[X][i]] <= 2) Y = pr[Y][i], X = pr[X][i];
smin(w, (D[pr[X][0]] > 2 ? Wtmp[pr[X][0] - M] : INF));
if (h[X] <= h[lca]) smin(w, max(Wtmp[X - M], ext[X]));
return w >= INF ? -1 : w;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12756 KB |
Output is correct |
2 |
Correct |
6 ms |
12756 KB |
Output is correct |
3 |
Correct |
7 ms |
12832 KB |
Output is correct |
4 |
Correct |
7 ms |
12884 KB |
Output is correct |
5 |
Correct |
7 ms |
13056 KB |
Output is correct |
6 |
Correct |
7 ms |
13012 KB |
Output is correct |
7 |
Correct |
9 ms |
13012 KB |
Output is correct |
8 |
Correct |
8 ms |
13148 KB |
Output is correct |
9 |
Correct |
105 ms |
31876 KB |
Output is correct |
10 |
Correct |
139 ms |
36172 KB |
Output is correct |
11 |
Correct |
107 ms |
35780 KB |
Output is correct |
12 |
Correct |
122 ms |
37112 KB |
Output is correct |
13 |
Correct |
129 ms |
41040 KB |
Output is correct |
14 |
Correct |
110 ms |
31968 KB |
Output is correct |
15 |
Correct |
242 ms |
38120 KB |
Output is correct |
16 |
Correct |
205 ms |
37440 KB |
Output is correct |
17 |
Correct |
217 ms |
38912 KB |
Output is correct |
18 |
Correct |
388 ms |
42804 KB |
Output is correct |
19 |
Correct |
80 ms |
20112 KB |
Output is correct |
20 |
Correct |
209 ms |
39368 KB |
Output is correct |
21 |
Correct |
253 ms |
38648 KB |
Output is correct |
22 |
Correct |
243 ms |
40316 KB |
Output is correct |
23 |
Correct |
334 ms |
44400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12756 KB |
Output is correct |
2 |
Correct |
6 ms |
12756 KB |
Output is correct |
3 |
Correct |
384 ms |
46148 KB |
Output is correct |
4 |
Correct |
307 ms |
47756 KB |
Output is correct |
5 |
Correct |
285 ms |
46864 KB |
Output is correct |
6 |
Correct |
406 ms |
47620 KB |
Output is correct |
7 |
Correct |
367 ms |
47320 KB |
Output is correct |
8 |
Correct |
310 ms |
45988 KB |
Output is correct |
9 |
Correct |
281 ms |
46980 KB |
Output is correct |
10 |
Correct |
338 ms |
45704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12756 KB |
Output is correct |
2 |
Correct |
6 ms |
12756 KB |
Output is correct |
3 |
Correct |
7 ms |
12832 KB |
Output is correct |
4 |
Correct |
7 ms |
12884 KB |
Output is correct |
5 |
Correct |
7 ms |
13056 KB |
Output is correct |
6 |
Correct |
7 ms |
13012 KB |
Output is correct |
7 |
Correct |
9 ms |
13012 KB |
Output is correct |
8 |
Correct |
8 ms |
13148 KB |
Output is correct |
9 |
Correct |
6 ms |
12756 KB |
Output is correct |
10 |
Correct |
7 ms |
13012 KB |
Output is correct |
11 |
Correct |
7 ms |
13104 KB |
Output is correct |
12 |
Correct |
7 ms |
13108 KB |
Output is correct |
13 |
Correct |
8 ms |
13012 KB |
Output is correct |
14 |
Correct |
7 ms |
13012 KB |
Output is correct |
15 |
Correct |
7 ms |
13012 KB |
Output is correct |
16 |
Correct |
7 ms |
13012 KB |
Output is correct |
17 |
Correct |
7 ms |
13140 KB |
Output is correct |
18 |
Correct |
7 ms |
13012 KB |
Output is correct |
19 |
Correct |
8 ms |
13012 KB |
Output is correct |
20 |
Correct |
8 ms |
13012 KB |
Output is correct |
21 |
Correct |
7 ms |
13132 KB |
Output is correct |
22 |
Correct |
8 ms |
13012 KB |
Output is correct |
23 |
Correct |
8 ms |
13012 KB |
Output is correct |
24 |
Correct |
7 ms |
13204 KB |
Output is correct |
25 |
Correct |
7 ms |
13140 KB |
Output is correct |
26 |
Correct |
7 ms |
13140 KB |
Output is correct |
27 |
Correct |
7 ms |
13140 KB |
Output is correct |
28 |
Correct |
7 ms |
13140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12756 KB |
Output is correct |
2 |
Correct |
8 ms |
12756 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12832 KB |
Output is correct |
5 |
Correct |
7 ms |
12884 KB |
Output is correct |
6 |
Correct |
7 ms |
13056 KB |
Output is correct |
7 |
Correct |
7 ms |
13012 KB |
Output is correct |
8 |
Correct |
9 ms |
13012 KB |
Output is correct |
9 |
Correct |
8 ms |
13148 KB |
Output is correct |
10 |
Correct |
105 ms |
31876 KB |
Output is correct |
11 |
Correct |
139 ms |
36172 KB |
Output is correct |
12 |
Correct |
107 ms |
35780 KB |
Output is correct |
13 |
Correct |
122 ms |
37112 KB |
Output is correct |
14 |
Correct |
129 ms |
41040 KB |
Output is correct |
15 |
Correct |
7 ms |
13012 KB |
Output is correct |
16 |
Correct |
7 ms |
13104 KB |
Output is correct |
17 |
Correct |
7 ms |
13108 KB |
Output is correct |
18 |
Correct |
8 ms |
13012 KB |
Output is correct |
19 |
Correct |
7 ms |
13012 KB |
Output is correct |
20 |
Correct |
7 ms |
13012 KB |
Output is correct |
21 |
Correct |
7 ms |
13012 KB |
Output is correct |
22 |
Correct |
7 ms |
13140 KB |
Output is correct |
23 |
Correct |
7 ms |
13012 KB |
Output is correct |
24 |
Correct |
8 ms |
13012 KB |
Output is correct |
25 |
Correct |
8 ms |
13012 KB |
Output is correct |
26 |
Correct |
7 ms |
13132 KB |
Output is correct |
27 |
Correct |
8 ms |
13012 KB |
Output is correct |
28 |
Correct |
8 ms |
13012 KB |
Output is correct |
29 |
Correct |
7 ms |
13204 KB |
Output is correct |
30 |
Correct |
7 ms |
13140 KB |
Output is correct |
31 |
Correct |
7 ms |
13140 KB |
Output is correct |
32 |
Correct |
7 ms |
13140 KB |
Output is correct |
33 |
Correct |
7 ms |
13140 KB |
Output is correct |
34 |
Correct |
16 ms |
16180 KB |
Output is correct |
35 |
Correct |
135 ms |
37036 KB |
Output is correct |
36 |
Correct |
135 ms |
37112 KB |
Output is correct |
37 |
Correct |
111 ms |
36976 KB |
Output is correct |
38 |
Correct |
129 ms |
36856 KB |
Output is correct |
39 |
Correct |
116 ms |
36592 KB |
Output is correct |
40 |
Correct |
123 ms |
34636 KB |
Output is correct |
41 |
Correct |
115 ms |
37052 KB |
Output is correct |
42 |
Correct |
125 ms |
37136 KB |
Output is correct |
43 |
Correct |
126 ms |
42288 KB |
Output is correct |
44 |
Correct |
154 ms |
37196 KB |
Output is correct |
45 |
Correct |
140 ms |
42160 KB |
Output is correct |
46 |
Correct |
113 ms |
37140 KB |
Output is correct |
47 |
Correct |
129 ms |
37196 KB |
Output is correct |
48 |
Correct |
144 ms |
40068 KB |
Output is correct |
49 |
Correct |
56 ms |
22956 KB |
Output is correct |
50 |
Correct |
53 ms |
28108 KB |
Output is correct |
51 |
Correct |
111 ms |
37704 KB |
Output is correct |
52 |
Correct |
168 ms |
44612 KB |
Output is correct |
53 |
Correct |
165 ms |
47520 KB |
Output is correct |
54 |
Correct |
170 ms |
49168 KB |
Output is correct |
55 |
Correct |
131 ms |
41804 KB |
Output is correct |
56 |
Correct |
186 ms |
48216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12756 KB |
Output is correct |
2 |
Correct |
6 ms |
12756 KB |
Output is correct |
3 |
Correct |
7 ms |
12832 KB |
Output is correct |
4 |
Correct |
7 ms |
12884 KB |
Output is correct |
5 |
Correct |
7 ms |
13056 KB |
Output is correct |
6 |
Correct |
7 ms |
13012 KB |
Output is correct |
7 |
Correct |
9 ms |
13012 KB |
Output is correct |
8 |
Correct |
8 ms |
13148 KB |
Output is correct |
9 |
Correct |
105 ms |
31876 KB |
Output is correct |
10 |
Correct |
139 ms |
36172 KB |
Output is correct |
11 |
Correct |
107 ms |
35780 KB |
Output is correct |
12 |
Correct |
122 ms |
37112 KB |
Output is correct |
13 |
Correct |
129 ms |
41040 KB |
Output is correct |
14 |
Correct |
110 ms |
31968 KB |
Output is correct |
15 |
Correct |
242 ms |
38120 KB |
Output is correct |
16 |
Correct |
205 ms |
37440 KB |
Output is correct |
17 |
Correct |
217 ms |
38912 KB |
Output is correct |
18 |
Correct |
388 ms |
42804 KB |
Output is correct |
19 |
Correct |
384 ms |
46148 KB |
Output is correct |
20 |
Correct |
307 ms |
47756 KB |
Output is correct |
21 |
Correct |
285 ms |
46864 KB |
Output is correct |
22 |
Correct |
406 ms |
47620 KB |
Output is correct |
23 |
Correct |
367 ms |
47320 KB |
Output is correct |
24 |
Correct |
310 ms |
45988 KB |
Output is correct |
25 |
Correct |
281 ms |
46980 KB |
Output is correct |
26 |
Correct |
338 ms |
45704 KB |
Output is correct |
27 |
Correct |
7 ms |
13012 KB |
Output is correct |
28 |
Correct |
7 ms |
13104 KB |
Output is correct |
29 |
Correct |
7 ms |
13108 KB |
Output is correct |
30 |
Correct |
8 ms |
13012 KB |
Output is correct |
31 |
Correct |
7 ms |
13012 KB |
Output is correct |
32 |
Correct |
7 ms |
13012 KB |
Output is correct |
33 |
Correct |
7 ms |
13012 KB |
Output is correct |
34 |
Correct |
7 ms |
13140 KB |
Output is correct |
35 |
Correct |
7 ms |
13012 KB |
Output is correct |
36 |
Correct |
16 ms |
16180 KB |
Output is correct |
37 |
Correct |
135 ms |
37036 KB |
Output is correct |
38 |
Correct |
135 ms |
37112 KB |
Output is correct |
39 |
Correct |
111 ms |
36976 KB |
Output is correct |
40 |
Correct |
129 ms |
36856 KB |
Output is correct |
41 |
Correct |
116 ms |
36592 KB |
Output is correct |
42 |
Correct |
123 ms |
34636 KB |
Output is correct |
43 |
Correct |
115 ms |
37052 KB |
Output is correct |
44 |
Correct |
125 ms |
37136 KB |
Output is correct |
45 |
Correct |
126 ms |
42288 KB |
Output is correct |
46 |
Correct |
154 ms |
37196 KB |
Output is correct |
47 |
Correct |
23 ms |
16116 KB |
Output is correct |
48 |
Correct |
258 ms |
39672 KB |
Output is correct |
49 |
Correct |
212 ms |
39716 KB |
Output is correct |
50 |
Correct |
226 ms |
39532 KB |
Output is correct |
51 |
Correct |
205 ms |
39532 KB |
Output is correct |
52 |
Correct |
264 ms |
38236 KB |
Output is correct |
53 |
Correct |
184 ms |
32512 KB |
Output is correct |
54 |
Correct |
239 ms |
40264 KB |
Output is correct |
55 |
Correct |
220 ms |
39644 KB |
Output is correct |
56 |
Correct |
312 ms |
43752 KB |
Output is correct |
57 |
Correct |
256 ms |
40464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12756 KB |
Output is correct |
2 |
Correct |
8 ms |
12756 KB |
Output is correct |
3 |
Correct |
6 ms |
12756 KB |
Output is correct |
4 |
Correct |
7 ms |
12832 KB |
Output is correct |
5 |
Correct |
7 ms |
12884 KB |
Output is correct |
6 |
Correct |
7 ms |
13056 KB |
Output is correct |
7 |
Correct |
7 ms |
13012 KB |
Output is correct |
8 |
Correct |
9 ms |
13012 KB |
Output is correct |
9 |
Correct |
8 ms |
13148 KB |
Output is correct |
10 |
Correct |
105 ms |
31876 KB |
Output is correct |
11 |
Correct |
139 ms |
36172 KB |
Output is correct |
12 |
Correct |
107 ms |
35780 KB |
Output is correct |
13 |
Correct |
122 ms |
37112 KB |
Output is correct |
14 |
Correct |
129 ms |
41040 KB |
Output is correct |
15 |
Correct |
110 ms |
31968 KB |
Output is correct |
16 |
Correct |
242 ms |
38120 KB |
Output is correct |
17 |
Correct |
205 ms |
37440 KB |
Output is correct |
18 |
Correct |
217 ms |
38912 KB |
Output is correct |
19 |
Correct |
388 ms |
42804 KB |
Output is correct |
20 |
Correct |
384 ms |
46148 KB |
Output is correct |
21 |
Correct |
307 ms |
47756 KB |
Output is correct |
22 |
Correct |
285 ms |
46864 KB |
Output is correct |
23 |
Correct |
406 ms |
47620 KB |
Output is correct |
24 |
Correct |
367 ms |
47320 KB |
Output is correct |
25 |
Correct |
310 ms |
45988 KB |
Output is correct |
26 |
Correct |
281 ms |
46980 KB |
Output is correct |
27 |
Correct |
338 ms |
45704 KB |
Output is correct |
28 |
Correct |
7 ms |
13012 KB |
Output is correct |
29 |
Correct |
7 ms |
13104 KB |
Output is correct |
30 |
Correct |
7 ms |
13108 KB |
Output is correct |
31 |
Correct |
8 ms |
13012 KB |
Output is correct |
32 |
Correct |
7 ms |
13012 KB |
Output is correct |
33 |
Correct |
7 ms |
13012 KB |
Output is correct |
34 |
Correct |
7 ms |
13012 KB |
Output is correct |
35 |
Correct |
7 ms |
13140 KB |
Output is correct |
36 |
Correct |
7 ms |
13012 KB |
Output is correct |
37 |
Correct |
16 ms |
16180 KB |
Output is correct |
38 |
Correct |
135 ms |
37036 KB |
Output is correct |
39 |
Correct |
135 ms |
37112 KB |
Output is correct |
40 |
Correct |
111 ms |
36976 KB |
Output is correct |
41 |
Correct |
129 ms |
36856 KB |
Output is correct |
42 |
Correct |
116 ms |
36592 KB |
Output is correct |
43 |
Correct |
123 ms |
34636 KB |
Output is correct |
44 |
Correct |
115 ms |
37052 KB |
Output is correct |
45 |
Correct |
125 ms |
37136 KB |
Output is correct |
46 |
Correct |
126 ms |
42288 KB |
Output is correct |
47 |
Correct |
154 ms |
37196 KB |
Output is correct |
48 |
Correct |
23 ms |
16116 KB |
Output is correct |
49 |
Correct |
258 ms |
39672 KB |
Output is correct |
50 |
Correct |
212 ms |
39716 KB |
Output is correct |
51 |
Correct |
226 ms |
39532 KB |
Output is correct |
52 |
Correct |
205 ms |
39532 KB |
Output is correct |
53 |
Correct |
264 ms |
38236 KB |
Output is correct |
54 |
Correct |
184 ms |
32512 KB |
Output is correct |
55 |
Correct |
239 ms |
40264 KB |
Output is correct |
56 |
Correct |
220 ms |
39644 KB |
Output is correct |
57 |
Correct |
312 ms |
43752 KB |
Output is correct |
58 |
Correct |
256 ms |
40464 KB |
Output is correct |
59 |
Correct |
80 ms |
20112 KB |
Output is correct |
60 |
Correct |
209 ms |
39368 KB |
Output is correct |
61 |
Correct |
253 ms |
38648 KB |
Output is correct |
62 |
Correct |
243 ms |
40316 KB |
Output is correct |
63 |
Correct |
334 ms |
44400 KB |
Output is correct |
64 |
Correct |
8 ms |
13012 KB |
Output is correct |
65 |
Correct |
8 ms |
13012 KB |
Output is correct |
66 |
Correct |
7 ms |
13132 KB |
Output is correct |
67 |
Correct |
8 ms |
13012 KB |
Output is correct |
68 |
Correct |
8 ms |
13012 KB |
Output is correct |
69 |
Correct |
7 ms |
13204 KB |
Output is correct |
70 |
Correct |
7 ms |
13140 KB |
Output is correct |
71 |
Correct |
7 ms |
13140 KB |
Output is correct |
72 |
Correct |
7 ms |
13140 KB |
Output is correct |
73 |
Correct |
7 ms |
13140 KB |
Output is correct |
74 |
Correct |
140 ms |
42160 KB |
Output is correct |
75 |
Correct |
113 ms |
37140 KB |
Output is correct |
76 |
Correct |
129 ms |
37196 KB |
Output is correct |
77 |
Correct |
144 ms |
40068 KB |
Output is correct |
78 |
Correct |
56 ms |
22956 KB |
Output is correct |
79 |
Correct |
53 ms |
28108 KB |
Output is correct |
80 |
Correct |
111 ms |
37704 KB |
Output is correct |
81 |
Correct |
168 ms |
44612 KB |
Output is correct |
82 |
Correct |
165 ms |
47520 KB |
Output is correct |
83 |
Correct |
170 ms |
49168 KB |
Output is correct |
84 |
Correct |
131 ms |
41804 KB |
Output is correct |
85 |
Correct |
186 ms |
48216 KB |
Output is correct |
86 |
Correct |
82 ms |
23864 KB |
Output is correct |
87 |
Correct |
208 ms |
39688 KB |
Output is correct |
88 |
Correct |
219 ms |
39592 KB |
Output is correct |
89 |
Correct |
294 ms |
41192 KB |
Output is correct |
90 |
Correct |
138 ms |
27304 KB |
Output is correct |
91 |
Correct |
186 ms |
34728 KB |
Output is correct |
92 |
Correct |
252 ms |
39880 KB |
Output is correct |
93 |
Correct |
307 ms |
46720 KB |
Output is correct |
94 |
Correct |
315 ms |
49400 KB |
Output is correct |
95 |
Correct |
285 ms |
50736 KB |
Output is correct |
96 |
Correct |
327 ms |
44032 KB |
Output is correct |
97 |
Correct |
332 ms |
46188 KB |
Output is correct |