#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int lab[N], order[N], deg[N], noline[N], mx[N], h[N];
int par[18][N], f[18][N];
int cur;
vector<int> lst[N];
vector<pair<int, int> > adj[N];
int root(int v) {
return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}
void update(int u) {
if (mx[u] <= 2) return;
for(int j : lst[u]) noline[j] = cur;
lst[u].clear();
}
bool join(int u, int v) {
u = root(u); v = root(v);
if (u == v) return 0;
if (lab[u] > lab[v]) swap(u, v);
mx[u] = max(mx[u], mx[v]);
lab[u] += lab[v];
for(int j : lst[v]) lst[u].push_back(j);
lab[v] = u;
return 1;
}
void dfs(int u) {
for(auto [v, w] : adj[u]) if (v != par[0][u]) {
par[0][v] = u;
f[0][v] = w;
for(int j = 1; j <= 17; j++) {
int p = par[j - 1][v];
par[j][v] = par[j - 1][p];
f[j][v] = max(f[j - 1][v], f[j - 1][p]);
}
h[v] = h[u] + 1;
dfs(v);
}
}
int lca(int u, int v) {
int ret = 0;
if (h[u] < h[v]) swap(u, v);
int delta = h[u] - h[v];
for(int j = 0; j <= 17; j++) if ((delta >> j) & 1) {
ret = max(ret, f[j][u]);
u = par[j][u];
}
if (u == v) return ret;
for(int j = 17; j >= 0; j--) if (par[j][u] != par[j][v]) {
ret = max(ret, f[j][u]);
ret = max(ret, f[j][v]);
u = par[j][u];
v = par[j][v];
}
return max({ret, f[0][u], f[0][v]});
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
for(int i = 0; i < n; i++) {
lab[i] = -1;
lst[i].push_back(i);
}
for(int i = 0; i < m; i++) order[i] = i;
sort(order, order + m, [&] (int &x, int &y) {
return w[x] < w[y];
});
for(int j = 0; j < m; j++) {
int i = order[j];
cur = w[i];
if (join(u[i], v[i])) {
adj[u[i]].push_back({v[i], w[i]});
adj[v[i]].push_back({u[i], w[i]});
}
else mx[root(u[i])] = 3;
int r = root(u[i]);
mx[r] = max(mx[r], max(++deg[u[i]], ++deg[v[i]]));
update(r);
}
dfs(0);
}
int getMinimumFuelCapacity(int x, int y) {
if (!noline[x]) return -1;
return max(noline[x], lca(x, y));
}
//int main() {
// #define task ""
// cin.tie(0) -> sync_with_stdio(0);
// if (fopen ("task.inp", "r")) {
// freopen ("task.inp", "r", stdin);
// freopen ("task.out", "w", stdout);
// }
// if (fopen (task".inp", "r")) {
// freopen (task".inp", "r", stdin);
// freopen (task".out", "w", stdout);
// }
//// int u[1000] = {0, 0, 1, 1, 1, 2};
//// int v[1000] = {1, 2, 2, 3, 4, 3};
//// int w[1000] = {4, 4, 1, 2, 10, 3};
// vector<int> u = {0, 0};
// vector<int> v = {1, 2};
// vector<int> w = {5, 5};
// init(3, 2, u, v, w);
// cout << getMinimumFuelCapacity(1, 2);
//}
Compilation message
swap.cpp: In function 'void dfs(int)':
swap.cpp:32:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for(auto [v, w] : adj[u]) if (v != par[0][u]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5336 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
118 ms |
30124 KB |
Output is correct |
10 |
Correct |
159 ms |
36580 KB |
Output is correct |
11 |
Correct |
164 ms |
35500 KB |
Output is correct |
12 |
Correct |
170 ms |
37540 KB |
Output is correct |
13 |
Correct |
132 ms |
35952 KB |
Output is correct |
14 |
Correct |
149 ms |
29840 KB |
Output is correct |
15 |
Correct |
240 ms |
38568 KB |
Output is correct |
16 |
Correct |
215 ms |
36320 KB |
Output is correct |
17 |
Correct |
238 ms |
40932 KB |
Output is correct |
18 |
Correct |
184 ms |
36632 KB |
Output is correct |
19 |
Correct |
76 ms |
13900 KB |
Output is correct |
20 |
Correct |
325 ms |
39356 KB |
Output is correct |
21 |
Correct |
366 ms |
37864 KB |
Output is correct |
22 |
Correct |
367 ms |
40876 KB |
Output is correct |
23 |
Correct |
328 ms |
38128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
182 ms |
32220 KB |
Output is correct |
4 |
Correct |
210 ms |
33296 KB |
Output is correct |
5 |
Correct |
193 ms |
32732 KB |
Output is correct |
6 |
Correct |
210 ms |
33116 KB |
Output is correct |
7 |
Correct |
187 ms |
33088 KB |
Output is correct |
8 |
Correct |
202 ms |
32072 KB |
Output is correct |
9 |
Correct |
205 ms |
32856 KB |
Output is correct |
10 |
Correct |
178 ms |
31752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5336 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
5148 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
4 ms |
5332 KB |
Output is correct |
12 |
Correct |
3 ms |
5332 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
4 ms |
5332 KB |
Output is correct |
15 |
Correct |
4 ms |
5428 KB |
Output is correct |
16 |
Correct |
3 ms |
5332 KB |
Output is correct |
17 |
Correct |
4 ms |
5332 KB |
Output is correct |
18 |
Correct |
3 ms |
5332 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
3 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5380 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
4 ms |
5332 KB |
Output is correct |
26 |
Correct |
4 ms |
5460 KB |
Output is correct |
27 |
Correct |
4 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5148 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5332 KB |
Output is correct |
8 |
Correct |
3 ms |
5336 KB |
Output is correct |
9 |
Correct |
4 ms |
5460 KB |
Output is correct |
10 |
Correct |
118 ms |
30124 KB |
Output is correct |
11 |
Correct |
159 ms |
36580 KB |
Output is correct |
12 |
Correct |
164 ms |
35500 KB |
Output is correct |
13 |
Correct |
170 ms |
37540 KB |
Output is correct |
14 |
Correct |
132 ms |
35952 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
3 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5332 KB |
Output is correct |
20 |
Correct |
4 ms |
5428 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5332 KB |
Output is correct |
23 |
Correct |
3 ms |
5332 KB |
Output is correct |
24 |
Correct |
3 ms |
5332 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
3 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5204 KB |
Output is correct |
28 |
Correct |
4 ms |
5380 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5332 KB |
Output is correct |
31 |
Correct |
4 ms |
5460 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
3 ms |
5332 KB |
Output is correct |
34 |
Correct |
13 ms |
8532 KB |
Output is correct |
35 |
Correct |
198 ms |
37016 KB |
Output is correct |
36 |
Correct |
206 ms |
34652 KB |
Output is correct |
37 |
Correct |
184 ms |
32272 KB |
Output is correct |
38 |
Correct |
183 ms |
31172 KB |
Output is correct |
39 |
Correct |
182 ms |
30356 KB |
Output is correct |
40 |
Correct |
173 ms |
28128 KB |
Output is correct |
41 |
Correct |
189 ms |
35876 KB |
Output is correct |
42 |
Correct |
194 ms |
37140 KB |
Output is correct |
43 |
Correct |
139 ms |
34736 KB |
Output is correct |
44 |
Correct |
153 ms |
30724 KB |
Output is correct |
45 |
Runtime error |
68 ms |
23368 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5336 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
118 ms |
30124 KB |
Output is correct |
10 |
Correct |
159 ms |
36580 KB |
Output is correct |
11 |
Correct |
164 ms |
35500 KB |
Output is correct |
12 |
Correct |
170 ms |
37540 KB |
Output is correct |
13 |
Correct |
132 ms |
35952 KB |
Output is correct |
14 |
Correct |
149 ms |
29840 KB |
Output is correct |
15 |
Correct |
240 ms |
38568 KB |
Output is correct |
16 |
Correct |
215 ms |
36320 KB |
Output is correct |
17 |
Correct |
238 ms |
40932 KB |
Output is correct |
18 |
Correct |
184 ms |
36632 KB |
Output is correct |
19 |
Correct |
182 ms |
32220 KB |
Output is correct |
20 |
Correct |
210 ms |
33296 KB |
Output is correct |
21 |
Correct |
193 ms |
32732 KB |
Output is correct |
22 |
Correct |
210 ms |
33116 KB |
Output is correct |
23 |
Correct |
187 ms |
33088 KB |
Output is correct |
24 |
Correct |
202 ms |
32072 KB |
Output is correct |
25 |
Correct |
205 ms |
32856 KB |
Output is correct |
26 |
Correct |
178 ms |
31752 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
29 |
Correct |
3 ms |
5332 KB |
Output is correct |
30 |
Correct |
3 ms |
5332 KB |
Output is correct |
31 |
Correct |
4 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5428 KB |
Output is correct |
33 |
Correct |
3 ms |
5332 KB |
Output is correct |
34 |
Correct |
4 ms |
5332 KB |
Output is correct |
35 |
Correct |
3 ms |
5332 KB |
Output is correct |
36 |
Correct |
13 ms |
8532 KB |
Output is correct |
37 |
Correct |
198 ms |
37016 KB |
Output is correct |
38 |
Correct |
206 ms |
34652 KB |
Output is correct |
39 |
Correct |
184 ms |
32272 KB |
Output is correct |
40 |
Correct |
183 ms |
31172 KB |
Output is correct |
41 |
Correct |
182 ms |
30356 KB |
Output is correct |
42 |
Correct |
173 ms |
28128 KB |
Output is correct |
43 |
Correct |
189 ms |
35876 KB |
Output is correct |
44 |
Correct |
194 ms |
37140 KB |
Output is correct |
45 |
Correct |
139 ms |
34736 KB |
Output is correct |
46 |
Correct |
153 ms |
30724 KB |
Output is correct |
47 |
Correct |
21 ms |
8660 KB |
Output is correct |
48 |
Correct |
398 ms |
38460 KB |
Output is correct |
49 |
Correct |
356 ms |
36084 KB |
Output is correct |
50 |
Correct |
395 ms |
34944 KB |
Output is correct |
51 |
Correct |
341 ms |
33944 KB |
Output is correct |
52 |
Correct |
353 ms |
31660 KB |
Output is correct |
53 |
Correct |
189 ms |
25652 KB |
Output is correct |
54 |
Correct |
420 ms |
38528 KB |
Output is correct |
55 |
Correct |
378 ms |
39180 KB |
Output is correct |
56 |
Correct |
296 ms |
37992 KB |
Output is correct |
57 |
Correct |
302 ms |
33828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5148 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5332 KB |
Output is correct |
8 |
Correct |
3 ms |
5336 KB |
Output is correct |
9 |
Correct |
4 ms |
5460 KB |
Output is correct |
10 |
Correct |
118 ms |
30124 KB |
Output is correct |
11 |
Correct |
159 ms |
36580 KB |
Output is correct |
12 |
Correct |
164 ms |
35500 KB |
Output is correct |
13 |
Correct |
170 ms |
37540 KB |
Output is correct |
14 |
Correct |
132 ms |
35952 KB |
Output is correct |
15 |
Correct |
149 ms |
29840 KB |
Output is correct |
16 |
Correct |
240 ms |
38568 KB |
Output is correct |
17 |
Correct |
215 ms |
36320 KB |
Output is correct |
18 |
Correct |
238 ms |
40932 KB |
Output is correct |
19 |
Correct |
184 ms |
36632 KB |
Output is correct |
20 |
Correct |
182 ms |
32220 KB |
Output is correct |
21 |
Correct |
210 ms |
33296 KB |
Output is correct |
22 |
Correct |
193 ms |
32732 KB |
Output is correct |
23 |
Correct |
210 ms |
33116 KB |
Output is correct |
24 |
Correct |
187 ms |
33088 KB |
Output is correct |
25 |
Correct |
202 ms |
32072 KB |
Output is correct |
26 |
Correct |
205 ms |
32856 KB |
Output is correct |
27 |
Correct |
178 ms |
31752 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5332 KB |
Output is correct |
30 |
Correct |
3 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5332 KB |
Output is correct |
33 |
Correct |
4 ms |
5428 KB |
Output is correct |
34 |
Correct |
3 ms |
5332 KB |
Output is correct |
35 |
Correct |
4 ms |
5332 KB |
Output is correct |
36 |
Correct |
3 ms |
5332 KB |
Output is correct |
37 |
Correct |
13 ms |
8532 KB |
Output is correct |
38 |
Correct |
198 ms |
37016 KB |
Output is correct |
39 |
Correct |
206 ms |
34652 KB |
Output is correct |
40 |
Correct |
184 ms |
32272 KB |
Output is correct |
41 |
Correct |
183 ms |
31172 KB |
Output is correct |
42 |
Correct |
182 ms |
30356 KB |
Output is correct |
43 |
Correct |
173 ms |
28128 KB |
Output is correct |
44 |
Correct |
189 ms |
35876 KB |
Output is correct |
45 |
Correct |
194 ms |
37140 KB |
Output is correct |
46 |
Correct |
139 ms |
34736 KB |
Output is correct |
47 |
Correct |
153 ms |
30724 KB |
Output is correct |
48 |
Correct |
21 ms |
8660 KB |
Output is correct |
49 |
Correct |
398 ms |
38460 KB |
Output is correct |
50 |
Correct |
356 ms |
36084 KB |
Output is correct |
51 |
Correct |
395 ms |
34944 KB |
Output is correct |
52 |
Correct |
341 ms |
33944 KB |
Output is correct |
53 |
Correct |
353 ms |
31660 KB |
Output is correct |
54 |
Correct |
189 ms |
25652 KB |
Output is correct |
55 |
Correct |
420 ms |
38528 KB |
Output is correct |
56 |
Correct |
378 ms |
39180 KB |
Output is correct |
57 |
Correct |
296 ms |
37992 KB |
Output is correct |
58 |
Correct |
302 ms |
33828 KB |
Output is correct |
59 |
Correct |
76 ms |
13900 KB |
Output is correct |
60 |
Correct |
325 ms |
39356 KB |
Output is correct |
61 |
Correct |
366 ms |
37864 KB |
Output is correct |
62 |
Correct |
367 ms |
40876 KB |
Output is correct |
63 |
Correct |
328 ms |
38128 KB |
Output is correct |
64 |
Correct |
3 ms |
5332 KB |
Output is correct |
65 |
Correct |
3 ms |
5332 KB |
Output is correct |
66 |
Correct |
3 ms |
5332 KB |
Output is correct |
67 |
Correct |
3 ms |
5204 KB |
Output is correct |
68 |
Correct |
4 ms |
5380 KB |
Output is correct |
69 |
Correct |
4 ms |
5332 KB |
Output is correct |
70 |
Correct |
4 ms |
5332 KB |
Output is correct |
71 |
Correct |
4 ms |
5460 KB |
Output is correct |
72 |
Correct |
4 ms |
5332 KB |
Output is correct |
73 |
Correct |
3 ms |
5332 KB |
Output is correct |
74 |
Runtime error |
68 ms |
23368 KB |
Execution killed with signal 11 |
75 |
Halted |
0 ms |
0 KB |
- |