#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int lg = 19;
int id, deg[N], ans[N], one[N], three[N], p[N], dep[N];
vector<int> adj[N];
vector<vector<int>> lift(N, vector<int> (lg));
int find(int u) {
return p[u] == u ? u : p[u] = find(p[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
p[v] = u;
one[u] += one[v];
three[u] += three[v];
}
void dfs(int u) {
for (int v : adj[u]) {
if (!ans[v]) ans[v] = ans[u];
dep[v] = dep[u] + 1;
dfs(v);
}
}
int jump(int s, int d) {
for (int i = lg - 1; i >= 0; i--) if (d & (1 << i)) s = lift[s][i];
return s;
}
int lca(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
v = jump(v, dep[v] - dep[u]);
if (u == v) return u;
for (int i = lg - 1; i >= 0; i--) {
int ut = lift[u][i], vt = lift[v][i];
if (ut != vt) u = ut, v = vt;
}
return lift[u][0];
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
vector<array<int, 3>> e;
for (int i = 0; i < m; i++) e.push_back({w[i], u[i], v[i]});
sort(e.begin(), e.end());
for (int i = 0; i < N; i++) p[i] = i;
id = n - 1;
for (auto& [w, u, v] : e) {
int fu = find(u), fv = find(v);
if (deg[u] == 1) one[fu]--;
if (deg[v] == 1) one[fv]--;
deg[u]++, deg[v]++;
if (deg[u] == 1) one[fu]++;
else if (deg[u] == 3) three[fu]++;
if (deg[v] == 1) one[fv]++;
else if (deg[v] == 3) three[fv]++;
u = fu, v = fv;
id++;
if (u == v) {
merge(id, u);
lift[u][0] = id;
adj[id].push_back(u);
// cout << id << " --> " << u << '\n';
} else {
merge(id, u);
merge(id, v);
lift[u][0] = lift[v][0] = id;
adj[id].push_back(u);
// cout << id << " --> " << u << '\n';
adj[id].push_back(v);
// cout << id << " --> " << v << '\n';
}
if (three[id]) ans[id] = w;
else if (!one[id]) ans[id] = w;
// cout << "value @ " << id << " = " << w << '\n';
}
for (int i = 0; i < N; i++) if (find(i) == i) lift[i][0] = i, dfs(i);
for (int j = 1; j < lg; j++) for (int i = 0; i < N; i++) {
lift[i][j] = lift[lift[i][j - 1]][j - 1];
}
}
int getMinimumFuelCapacity(int X, int Y) {
if (find(X) != find(Y)) return -1;
// cout << lca(X, Y) << '\n';
int l = lca(X, Y);
if (ans[l] == 0) return -1;
return ans[l];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
48392 KB |
Output is correct |
2 |
Correct |
71 ms |
48140 KB |
Output is correct |
3 |
Correct |
67 ms |
47952 KB |
Output is correct |
4 |
Correct |
62 ms |
48104 KB |
Output is correct |
5 |
Correct |
64 ms |
48216 KB |
Output is correct |
6 |
Correct |
67 ms |
48224 KB |
Output is correct |
7 |
Correct |
74 ms |
48220 KB |
Output is correct |
8 |
Correct |
64 ms |
48220 KB |
Output is correct |
9 |
Correct |
119 ms |
53428 KB |
Output is correct |
10 |
Correct |
136 ms |
54732 KB |
Output is correct |
11 |
Correct |
138 ms |
54784 KB |
Output is correct |
12 |
Correct |
145 ms |
55196 KB |
Output is correct |
13 |
Correct |
168 ms |
57368 KB |
Output is correct |
14 |
Correct |
138 ms |
55432 KB |
Output is correct |
15 |
Correct |
247 ms |
60876 KB |
Output is correct |
16 |
Correct |
235 ms |
60364 KB |
Output is correct |
17 |
Correct |
254 ms |
61096 KB |
Output is correct |
18 |
Correct |
345 ms |
63504 KB |
Output is correct |
19 |
Correct |
132 ms |
54472 KB |
Output is correct |
20 |
Correct |
242 ms |
60872 KB |
Output is correct |
21 |
Correct |
252 ms |
60928 KB |
Output is correct |
22 |
Correct |
267 ms |
61432 KB |
Output is correct |
23 |
Correct |
345 ms |
63984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
48392 KB |
Output is correct |
2 |
Correct |
71 ms |
48140 KB |
Output is correct |
3 |
Correct |
288 ms |
60960 KB |
Output is correct |
4 |
Correct |
290 ms |
65360 KB |
Output is correct |
5 |
Correct |
305 ms |
65284 KB |
Output is correct |
6 |
Correct |
293 ms |
65176 KB |
Output is correct |
7 |
Correct |
313 ms |
65632 KB |
Output is correct |
8 |
Correct |
295 ms |
64748 KB |
Output is correct |
9 |
Correct |
389 ms |
65148 KB |
Output is correct |
10 |
Correct |
288 ms |
64916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
48392 KB |
Output is correct |
2 |
Correct |
71 ms |
48140 KB |
Output is correct |
3 |
Correct |
67 ms |
47952 KB |
Output is correct |
4 |
Correct |
62 ms |
48104 KB |
Output is correct |
5 |
Correct |
64 ms |
48216 KB |
Output is correct |
6 |
Correct |
67 ms |
48224 KB |
Output is correct |
7 |
Correct |
74 ms |
48220 KB |
Output is correct |
8 |
Correct |
64 ms |
48220 KB |
Output is correct |
9 |
Correct |
63 ms |
47960 KB |
Output is correct |
10 |
Correct |
64 ms |
48232 KB |
Output is correct |
11 |
Correct |
67 ms |
48216 KB |
Output is correct |
12 |
Correct |
65 ms |
48216 KB |
Output is correct |
13 |
Correct |
65 ms |
48168 KB |
Output is correct |
14 |
Correct |
61 ms |
48220 KB |
Output is correct |
15 |
Correct |
66 ms |
48220 KB |
Output is correct |
16 |
Correct |
63 ms |
48220 KB |
Output is correct |
17 |
Correct |
65 ms |
48160 KB |
Output is correct |
18 |
Correct |
65 ms |
48216 KB |
Output is correct |
19 |
Correct |
63 ms |
48220 KB |
Output is correct |
20 |
Correct |
70 ms |
48216 KB |
Output is correct |
21 |
Correct |
66 ms |
48220 KB |
Output is correct |
22 |
Correct |
65 ms |
48220 KB |
Output is correct |
23 |
Correct |
67 ms |
48216 KB |
Output is correct |
24 |
Correct |
67 ms |
48220 KB |
Output is correct |
25 |
Correct |
67 ms |
48216 KB |
Output is correct |
26 |
Correct |
64 ms |
48216 KB |
Output is correct |
27 |
Correct |
68 ms |
48220 KB |
Output is correct |
28 |
Correct |
76 ms |
48216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
47960 KB |
Output is correct |
2 |
Correct |
71 ms |
48392 KB |
Output is correct |
3 |
Correct |
71 ms |
48140 KB |
Output is correct |
4 |
Correct |
67 ms |
47952 KB |
Output is correct |
5 |
Correct |
62 ms |
48104 KB |
Output is correct |
6 |
Correct |
64 ms |
48216 KB |
Output is correct |
7 |
Correct |
67 ms |
48224 KB |
Output is correct |
8 |
Correct |
74 ms |
48220 KB |
Output is correct |
9 |
Correct |
64 ms |
48220 KB |
Output is correct |
10 |
Correct |
119 ms |
53428 KB |
Output is correct |
11 |
Correct |
136 ms |
54732 KB |
Output is correct |
12 |
Correct |
138 ms |
54784 KB |
Output is correct |
13 |
Correct |
145 ms |
55196 KB |
Output is correct |
14 |
Correct |
168 ms |
57368 KB |
Output is correct |
15 |
Correct |
64 ms |
48232 KB |
Output is correct |
16 |
Correct |
67 ms |
48216 KB |
Output is correct |
17 |
Correct |
65 ms |
48216 KB |
Output is correct |
18 |
Correct |
65 ms |
48168 KB |
Output is correct |
19 |
Correct |
61 ms |
48220 KB |
Output is correct |
20 |
Correct |
66 ms |
48220 KB |
Output is correct |
21 |
Correct |
63 ms |
48220 KB |
Output is correct |
22 |
Correct |
65 ms |
48160 KB |
Output is correct |
23 |
Correct |
65 ms |
48216 KB |
Output is correct |
24 |
Correct |
63 ms |
48220 KB |
Output is correct |
25 |
Correct |
70 ms |
48216 KB |
Output is correct |
26 |
Correct |
66 ms |
48220 KB |
Output is correct |
27 |
Correct |
65 ms |
48220 KB |
Output is correct |
28 |
Correct |
67 ms |
48216 KB |
Output is correct |
29 |
Correct |
67 ms |
48220 KB |
Output is correct |
30 |
Correct |
67 ms |
48216 KB |
Output is correct |
31 |
Correct |
64 ms |
48216 KB |
Output is correct |
32 |
Correct |
68 ms |
48220 KB |
Output is correct |
33 |
Correct |
76 ms |
48216 KB |
Output is correct |
34 |
Correct |
76 ms |
49380 KB |
Output is correct |
35 |
Correct |
144 ms |
56836 KB |
Output is correct |
36 |
Correct |
138 ms |
56964 KB |
Output is correct |
37 |
Correct |
134 ms |
56988 KB |
Output is correct |
38 |
Correct |
133 ms |
56780 KB |
Output is correct |
39 |
Correct |
136 ms |
56780 KB |
Output is correct |
40 |
Correct |
148 ms |
56012 KB |
Output is correct |
41 |
Correct |
138 ms |
57100 KB |
Output is correct |
42 |
Correct |
153 ms |
57104 KB |
Output is correct |
43 |
Correct |
138 ms |
59924 KB |
Output is correct |
44 |
Correct |
170 ms |
57236 KB |
Output is correct |
45 |
Correct |
169 ms |
67016 KB |
Output is correct |
46 |
Correct |
136 ms |
56944 KB |
Output is correct |
47 |
Correct |
141 ms |
57084 KB |
Output is correct |
48 |
Correct |
152 ms |
59588 KB |
Output is correct |
49 |
Correct |
140 ms |
68804 KB |
Output is correct |
50 |
Correct |
123 ms |
63736 KB |
Output is correct |
51 |
Correct |
159 ms |
63516 KB |
Output is correct |
52 |
Correct |
194 ms |
65728 KB |
Output is correct |
53 |
Correct |
196 ms |
67108 KB |
Output is correct |
54 |
Correct |
195 ms |
70752 KB |
Output is correct |
55 |
Correct |
150 ms |
59852 KB |
Output is correct |
56 |
Correct |
194 ms |
69064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
48392 KB |
Output is correct |
2 |
Correct |
71 ms |
48140 KB |
Output is correct |
3 |
Correct |
67 ms |
47952 KB |
Output is correct |
4 |
Correct |
62 ms |
48104 KB |
Output is correct |
5 |
Correct |
64 ms |
48216 KB |
Output is correct |
6 |
Correct |
67 ms |
48224 KB |
Output is correct |
7 |
Correct |
74 ms |
48220 KB |
Output is correct |
8 |
Correct |
64 ms |
48220 KB |
Output is correct |
9 |
Correct |
119 ms |
53428 KB |
Output is correct |
10 |
Correct |
136 ms |
54732 KB |
Output is correct |
11 |
Correct |
138 ms |
54784 KB |
Output is correct |
12 |
Correct |
145 ms |
55196 KB |
Output is correct |
13 |
Correct |
168 ms |
57368 KB |
Output is correct |
14 |
Correct |
138 ms |
55432 KB |
Output is correct |
15 |
Correct |
247 ms |
60876 KB |
Output is correct |
16 |
Correct |
235 ms |
60364 KB |
Output is correct |
17 |
Correct |
254 ms |
61096 KB |
Output is correct |
18 |
Correct |
345 ms |
63504 KB |
Output is correct |
19 |
Correct |
288 ms |
60960 KB |
Output is correct |
20 |
Correct |
290 ms |
65360 KB |
Output is correct |
21 |
Correct |
305 ms |
65284 KB |
Output is correct |
22 |
Correct |
293 ms |
65176 KB |
Output is correct |
23 |
Correct |
313 ms |
65632 KB |
Output is correct |
24 |
Correct |
295 ms |
64748 KB |
Output is correct |
25 |
Correct |
389 ms |
65148 KB |
Output is correct |
26 |
Correct |
288 ms |
64916 KB |
Output is correct |
27 |
Correct |
64 ms |
48232 KB |
Output is correct |
28 |
Correct |
67 ms |
48216 KB |
Output is correct |
29 |
Correct |
65 ms |
48216 KB |
Output is correct |
30 |
Correct |
65 ms |
48168 KB |
Output is correct |
31 |
Correct |
61 ms |
48220 KB |
Output is correct |
32 |
Correct |
66 ms |
48220 KB |
Output is correct |
33 |
Correct |
63 ms |
48220 KB |
Output is correct |
34 |
Correct |
65 ms |
48160 KB |
Output is correct |
35 |
Correct |
65 ms |
48216 KB |
Output is correct |
36 |
Correct |
76 ms |
49380 KB |
Output is correct |
37 |
Correct |
144 ms |
56836 KB |
Output is correct |
38 |
Correct |
138 ms |
56964 KB |
Output is correct |
39 |
Correct |
134 ms |
56988 KB |
Output is correct |
40 |
Correct |
133 ms |
56780 KB |
Output is correct |
41 |
Correct |
136 ms |
56780 KB |
Output is correct |
42 |
Correct |
148 ms |
56012 KB |
Output is correct |
43 |
Correct |
138 ms |
57100 KB |
Output is correct |
44 |
Correct |
153 ms |
57104 KB |
Output is correct |
45 |
Correct |
138 ms |
59924 KB |
Output is correct |
46 |
Correct |
170 ms |
57236 KB |
Output is correct |
47 |
Correct |
79 ms |
49624 KB |
Output is correct |
48 |
Correct |
248 ms |
60872 KB |
Output is correct |
49 |
Correct |
242 ms |
60876 KB |
Output is correct |
50 |
Correct |
240 ms |
60872 KB |
Output is correct |
51 |
Correct |
241 ms |
60960 KB |
Output is correct |
52 |
Correct |
230 ms |
60224 KB |
Output is correct |
53 |
Correct |
209 ms |
59308 KB |
Output is correct |
54 |
Correct |
265 ms |
61592 KB |
Output is correct |
55 |
Correct |
259 ms |
60812 KB |
Output is correct |
56 |
Correct |
343 ms |
63180 KB |
Output is correct |
57 |
Correct |
246 ms |
61508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
47960 KB |
Output is correct |
2 |
Correct |
71 ms |
48392 KB |
Output is correct |
3 |
Correct |
71 ms |
48140 KB |
Output is correct |
4 |
Correct |
67 ms |
47952 KB |
Output is correct |
5 |
Correct |
62 ms |
48104 KB |
Output is correct |
6 |
Correct |
64 ms |
48216 KB |
Output is correct |
7 |
Correct |
67 ms |
48224 KB |
Output is correct |
8 |
Correct |
74 ms |
48220 KB |
Output is correct |
9 |
Correct |
64 ms |
48220 KB |
Output is correct |
10 |
Correct |
119 ms |
53428 KB |
Output is correct |
11 |
Correct |
136 ms |
54732 KB |
Output is correct |
12 |
Correct |
138 ms |
54784 KB |
Output is correct |
13 |
Correct |
145 ms |
55196 KB |
Output is correct |
14 |
Correct |
168 ms |
57368 KB |
Output is correct |
15 |
Correct |
138 ms |
55432 KB |
Output is correct |
16 |
Correct |
247 ms |
60876 KB |
Output is correct |
17 |
Correct |
235 ms |
60364 KB |
Output is correct |
18 |
Correct |
254 ms |
61096 KB |
Output is correct |
19 |
Correct |
345 ms |
63504 KB |
Output is correct |
20 |
Correct |
288 ms |
60960 KB |
Output is correct |
21 |
Correct |
290 ms |
65360 KB |
Output is correct |
22 |
Correct |
305 ms |
65284 KB |
Output is correct |
23 |
Correct |
293 ms |
65176 KB |
Output is correct |
24 |
Correct |
313 ms |
65632 KB |
Output is correct |
25 |
Correct |
295 ms |
64748 KB |
Output is correct |
26 |
Correct |
389 ms |
65148 KB |
Output is correct |
27 |
Correct |
288 ms |
64916 KB |
Output is correct |
28 |
Correct |
64 ms |
48232 KB |
Output is correct |
29 |
Correct |
67 ms |
48216 KB |
Output is correct |
30 |
Correct |
65 ms |
48216 KB |
Output is correct |
31 |
Correct |
65 ms |
48168 KB |
Output is correct |
32 |
Correct |
61 ms |
48220 KB |
Output is correct |
33 |
Correct |
66 ms |
48220 KB |
Output is correct |
34 |
Correct |
63 ms |
48220 KB |
Output is correct |
35 |
Correct |
65 ms |
48160 KB |
Output is correct |
36 |
Correct |
65 ms |
48216 KB |
Output is correct |
37 |
Correct |
76 ms |
49380 KB |
Output is correct |
38 |
Correct |
144 ms |
56836 KB |
Output is correct |
39 |
Correct |
138 ms |
56964 KB |
Output is correct |
40 |
Correct |
134 ms |
56988 KB |
Output is correct |
41 |
Correct |
133 ms |
56780 KB |
Output is correct |
42 |
Correct |
136 ms |
56780 KB |
Output is correct |
43 |
Correct |
148 ms |
56012 KB |
Output is correct |
44 |
Correct |
138 ms |
57100 KB |
Output is correct |
45 |
Correct |
153 ms |
57104 KB |
Output is correct |
46 |
Correct |
138 ms |
59924 KB |
Output is correct |
47 |
Correct |
170 ms |
57236 KB |
Output is correct |
48 |
Correct |
79 ms |
49624 KB |
Output is correct |
49 |
Correct |
248 ms |
60872 KB |
Output is correct |
50 |
Correct |
242 ms |
60876 KB |
Output is correct |
51 |
Correct |
240 ms |
60872 KB |
Output is correct |
52 |
Correct |
241 ms |
60960 KB |
Output is correct |
53 |
Correct |
230 ms |
60224 KB |
Output is correct |
54 |
Correct |
209 ms |
59308 KB |
Output is correct |
55 |
Correct |
265 ms |
61592 KB |
Output is correct |
56 |
Correct |
259 ms |
60812 KB |
Output is correct |
57 |
Correct |
343 ms |
63180 KB |
Output is correct |
58 |
Correct |
246 ms |
61508 KB |
Output is correct |
59 |
Correct |
132 ms |
54472 KB |
Output is correct |
60 |
Correct |
242 ms |
60872 KB |
Output is correct |
61 |
Correct |
252 ms |
60928 KB |
Output is correct |
62 |
Correct |
267 ms |
61432 KB |
Output is correct |
63 |
Correct |
345 ms |
63984 KB |
Output is correct |
64 |
Correct |
63 ms |
48220 KB |
Output is correct |
65 |
Correct |
70 ms |
48216 KB |
Output is correct |
66 |
Correct |
66 ms |
48220 KB |
Output is correct |
67 |
Correct |
65 ms |
48220 KB |
Output is correct |
68 |
Correct |
67 ms |
48216 KB |
Output is correct |
69 |
Correct |
67 ms |
48220 KB |
Output is correct |
70 |
Correct |
67 ms |
48216 KB |
Output is correct |
71 |
Correct |
64 ms |
48216 KB |
Output is correct |
72 |
Correct |
68 ms |
48220 KB |
Output is correct |
73 |
Correct |
76 ms |
48216 KB |
Output is correct |
74 |
Correct |
169 ms |
67016 KB |
Output is correct |
75 |
Correct |
136 ms |
56944 KB |
Output is correct |
76 |
Correct |
141 ms |
57084 KB |
Output is correct |
77 |
Correct |
152 ms |
59588 KB |
Output is correct |
78 |
Correct |
140 ms |
68804 KB |
Output is correct |
79 |
Correct |
123 ms |
63736 KB |
Output is correct |
80 |
Correct |
159 ms |
63516 KB |
Output is correct |
81 |
Correct |
194 ms |
65728 KB |
Output is correct |
82 |
Correct |
196 ms |
67108 KB |
Output is correct |
83 |
Correct |
195 ms |
70752 KB |
Output is correct |
84 |
Correct |
150 ms |
59852 KB |
Output is correct |
85 |
Correct |
194 ms |
69064 KB |
Output is correct |
86 |
Correct |
127 ms |
54680 KB |
Output is correct |
87 |
Correct |
250 ms |
60876 KB |
Output is correct |
88 |
Correct |
234 ms |
60836 KB |
Output is correct |
89 |
Correct |
307 ms |
63180 KB |
Output is correct |
90 |
Correct |
212 ms |
73160 KB |
Output is correct |
91 |
Correct |
223 ms |
71104 KB |
Output is correct |
92 |
Correct |
319 ms |
66440 KB |
Output is correct |
93 |
Correct |
268 ms |
69196 KB |
Output is correct |
94 |
Correct |
353 ms |
71812 KB |
Output is correct |
95 |
Correct |
297 ms |
74688 KB |
Output is correct |
96 |
Correct |
331 ms |
63508 KB |
Output is correct |
97 |
Correct |
362 ms |
67364 KB |
Output is correct |