#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[20][N], f[20][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 |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5264 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5420 KB |
Output is correct |
8 |
Correct |
3 ms |
5460 KB |
Output is correct |
9 |
Correct |
121 ms |
31796 KB |
Output is correct |
10 |
Correct |
176 ms |
38608 KB |
Output is correct |
11 |
Correct |
189 ms |
37528 KB |
Output is correct |
12 |
Correct |
179 ms |
39624 KB |
Output is correct |
13 |
Correct |
132 ms |
38060 KB |
Output is correct |
14 |
Correct |
128 ms |
31636 KB |
Output is correct |
15 |
Correct |
259 ms |
42860 KB |
Output is correct |
16 |
Correct |
205 ms |
40636 KB |
Output is correct |
17 |
Correct |
225 ms |
45304 KB |
Output is correct |
18 |
Correct |
182 ms |
41072 KB |
Output is correct |
19 |
Correct |
79 ms |
15896 KB |
Output is correct |
20 |
Correct |
330 ms |
43764 KB |
Output is correct |
21 |
Correct |
327 ms |
42136 KB |
Output is correct |
22 |
Correct |
356 ms |
45244 KB |
Output is correct |
23 |
Correct |
302 ms |
42472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
205 ms |
36028 KB |
Output is correct |
4 |
Correct |
184 ms |
37108 KB |
Output is correct |
5 |
Correct |
194 ms |
36756 KB |
Output is correct |
6 |
Correct |
183 ms |
36960 KB |
Output is correct |
7 |
Correct |
213 ms |
36976 KB |
Output is correct |
8 |
Correct |
181 ms |
35944 KB |
Output is correct |
9 |
Correct |
186 ms |
36752 KB |
Output is correct |
10 |
Correct |
190 ms |
35680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5264 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5420 KB |
Output is correct |
8 |
Correct |
3 ms |
5460 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 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 |
3 ms |
5408 KB |
Output is correct |
15 |
Correct |
4 ms |
5400 KB |
Output is correct |
16 |
Correct |
3 ms |
5460 KB |
Output is correct |
17 |
Correct |
3 ms |
5460 KB |
Output is correct |
18 |
Correct |
3 ms |
5400 KB |
Output is correct |
19 |
Correct |
3 ms |
5276 KB |
Output is correct |
20 |
Correct |
4 ms |
5460 KB |
Output is correct |
21 |
Correct |
3 ms |
5396 KB |
Output is correct |
22 |
Correct |
3 ms |
5276 KB |
Output is correct |
23 |
Correct |
3 ms |
5332 KB |
Output is correct |
24 |
Correct |
4 ms |
5460 KB |
Output is correct |
25 |
Correct |
4 ms |
5400 KB |
Output is correct |
26 |
Correct |
4 ms |
5400 KB |
Output is correct |
27 |
Correct |
3 ms |
5460 KB |
Output is correct |
28 |
Correct |
4 ms |
5404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5152 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5264 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5420 KB |
Output is correct |
9 |
Correct |
3 ms |
5460 KB |
Output is correct |
10 |
Correct |
121 ms |
31796 KB |
Output is correct |
11 |
Correct |
176 ms |
38608 KB |
Output is correct |
12 |
Correct |
189 ms |
37528 KB |
Output is correct |
13 |
Correct |
179 ms |
39624 KB |
Output is correct |
14 |
Correct |
132 ms |
38060 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 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 |
3 ms |
5408 KB |
Output is correct |
20 |
Correct |
4 ms |
5400 KB |
Output is correct |
21 |
Correct |
3 ms |
5460 KB |
Output is correct |
22 |
Correct |
3 ms |
5460 KB |
Output is correct |
23 |
Correct |
3 ms |
5400 KB |
Output is correct |
24 |
Correct |
3 ms |
5276 KB |
Output is correct |
25 |
Correct |
4 ms |
5460 KB |
Output is correct |
26 |
Correct |
3 ms |
5396 KB |
Output is correct |
27 |
Correct |
3 ms |
5276 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
29 |
Correct |
4 ms |
5460 KB |
Output is correct |
30 |
Correct |
4 ms |
5400 KB |
Output is correct |
31 |
Correct |
4 ms |
5400 KB |
Output is correct |
32 |
Correct |
3 ms |
5460 KB |
Output is correct |
33 |
Correct |
4 ms |
5404 KB |
Output is correct |
34 |
Correct |
18 ms |
8916 KB |
Output is correct |
35 |
Correct |
201 ms |
38816 KB |
Output is correct |
36 |
Correct |
167 ms |
36392 KB |
Output is correct |
37 |
Correct |
175 ms |
34080 KB |
Output is correct |
38 |
Correct |
154 ms |
32904 KB |
Output is correct |
39 |
Correct |
150 ms |
32024 KB |
Output is correct |
40 |
Correct |
135 ms |
29944 KB |
Output is correct |
41 |
Correct |
171 ms |
37976 KB |
Output is correct |
42 |
Correct |
166 ms |
38932 KB |
Output is correct |
43 |
Correct |
131 ms |
36632 KB |
Output is correct |
44 |
Correct |
147 ms |
33048 KB |
Output is correct |
45 |
Runtime error |
74 ms |
26420 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5152 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5264 KB |
Output is correct |
5 |
Correct |
4 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
4 ms |
5420 KB |
Output is correct |
8 |
Correct |
3 ms |
5460 KB |
Output is correct |
9 |
Correct |
121 ms |
31796 KB |
Output is correct |
10 |
Correct |
176 ms |
38608 KB |
Output is correct |
11 |
Correct |
189 ms |
37528 KB |
Output is correct |
12 |
Correct |
179 ms |
39624 KB |
Output is correct |
13 |
Correct |
132 ms |
38060 KB |
Output is correct |
14 |
Correct |
128 ms |
31636 KB |
Output is correct |
15 |
Correct |
259 ms |
42860 KB |
Output is correct |
16 |
Correct |
205 ms |
40636 KB |
Output is correct |
17 |
Correct |
225 ms |
45304 KB |
Output is correct |
18 |
Correct |
182 ms |
41072 KB |
Output is correct |
19 |
Correct |
205 ms |
36028 KB |
Output is correct |
20 |
Correct |
184 ms |
37108 KB |
Output is correct |
21 |
Correct |
194 ms |
36756 KB |
Output is correct |
22 |
Correct |
183 ms |
36960 KB |
Output is correct |
23 |
Correct |
213 ms |
36976 KB |
Output is correct |
24 |
Correct |
181 ms |
35944 KB |
Output is correct |
25 |
Correct |
186 ms |
36752 KB |
Output is correct |
26 |
Correct |
190 ms |
35680 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 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 |
3 ms |
5408 KB |
Output is correct |
32 |
Correct |
4 ms |
5400 KB |
Output is correct |
33 |
Correct |
3 ms |
5460 KB |
Output is correct |
34 |
Correct |
3 ms |
5460 KB |
Output is correct |
35 |
Correct |
3 ms |
5400 KB |
Output is correct |
36 |
Correct |
18 ms |
8916 KB |
Output is correct |
37 |
Correct |
201 ms |
38816 KB |
Output is correct |
38 |
Correct |
167 ms |
36392 KB |
Output is correct |
39 |
Correct |
175 ms |
34080 KB |
Output is correct |
40 |
Correct |
154 ms |
32904 KB |
Output is correct |
41 |
Correct |
150 ms |
32024 KB |
Output is correct |
42 |
Correct |
135 ms |
29944 KB |
Output is correct |
43 |
Correct |
171 ms |
37976 KB |
Output is correct |
44 |
Correct |
166 ms |
38932 KB |
Output is correct |
45 |
Correct |
131 ms |
36632 KB |
Output is correct |
46 |
Correct |
147 ms |
33048 KB |
Output is correct |
47 |
Correct |
21 ms |
9096 KB |
Output is correct |
48 |
Correct |
364 ms |
42488 KB |
Output is correct |
49 |
Correct |
356 ms |
40328 KB |
Output is correct |
50 |
Correct |
358 ms |
39008 KB |
Output is correct |
51 |
Correct |
351 ms |
38240 KB |
Output is correct |
52 |
Correct |
307 ms |
35640 KB |
Output is correct |
53 |
Correct |
192 ms |
29308 KB |
Output is correct |
54 |
Correct |
374 ms |
43000 KB |
Output is correct |
55 |
Correct |
344 ms |
43300 KB |
Output is correct |
56 |
Correct |
306 ms |
41984 KB |
Output is correct |
57 |
Correct |
351 ms |
38332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5152 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5264 KB |
Output is correct |
6 |
Correct |
4 ms |
5332 KB |
Output is correct |
7 |
Correct |
3 ms |
5332 KB |
Output is correct |
8 |
Correct |
4 ms |
5420 KB |
Output is correct |
9 |
Correct |
3 ms |
5460 KB |
Output is correct |
10 |
Correct |
121 ms |
31796 KB |
Output is correct |
11 |
Correct |
176 ms |
38608 KB |
Output is correct |
12 |
Correct |
189 ms |
37528 KB |
Output is correct |
13 |
Correct |
179 ms |
39624 KB |
Output is correct |
14 |
Correct |
132 ms |
38060 KB |
Output is correct |
15 |
Correct |
128 ms |
31636 KB |
Output is correct |
16 |
Correct |
259 ms |
42860 KB |
Output is correct |
17 |
Correct |
205 ms |
40636 KB |
Output is correct |
18 |
Correct |
225 ms |
45304 KB |
Output is correct |
19 |
Correct |
182 ms |
41072 KB |
Output is correct |
20 |
Correct |
205 ms |
36028 KB |
Output is correct |
21 |
Correct |
184 ms |
37108 KB |
Output is correct |
22 |
Correct |
194 ms |
36756 KB |
Output is correct |
23 |
Correct |
183 ms |
36960 KB |
Output is correct |
24 |
Correct |
213 ms |
36976 KB |
Output is correct |
25 |
Correct |
181 ms |
35944 KB |
Output is correct |
26 |
Correct |
186 ms |
36752 KB |
Output is correct |
27 |
Correct |
190 ms |
35680 KB |
Output is correct |
28 |
Correct |
3 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 |
3 ms |
5332 KB |
Output is correct |
32 |
Correct |
3 ms |
5408 KB |
Output is correct |
33 |
Correct |
4 ms |
5400 KB |
Output is correct |
34 |
Correct |
3 ms |
5460 KB |
Output is correct |
35 |
Correct |
3 ms |
5460 KB |
Output is correct |
36 |
Correct |
3 ms |
5400 KB |
Output is correct |
37 |
Correct |
18 ms |
8916 KB |
Output is correct |
38 |
Correct |
201 ms |
38816 KB |
Output is correct |
39 |
Correct |
167 ms |
36392 KB |
Output is correct |
40 |
Correct |
175 ms |
34080 KB |
Output is correct |
41 |
Correct |
154 ms |
32904 KB |
Output is correct |
42 |
Correct |
150 ms |
32024 KB |
Output is correct |
43 |
Correct |
135 ms |
29944 KB |
Output is correct |
44 |
Correct |
171 ms |
37976 KB |
Output is correct |
45 |
Correct |
166 ms |
38932 KB |
Output is correct |
46 |
Correct |
131 ms |
36632 KB |
Output is correct |
47 |
Correct |
147 ms |
33048 KB |
Output is correct |
48 |
Correct |
21 ms |
9096 KB |
Output is correct |
49 |
Correct |
364 ms |
42488 KB |
Output is correct |
50 |
Correct |
356 ms |
40328 KB |
Output is correct |
51 |
Correct |
358 ms |
39008 KB |
Output is correct |
52 |
Correct |
351 ms |
38240 KB |
Output is correct |
53 |
Correct |
307 ms |
35640 KB |
Output is correct |
54 |
Correct |
192 ms |
29308 KB |
Output is correct |
55 |
Correct |
374 ms |
43000 KB |
Output is correct |
56 |
Correct |
344 ms |
43300 KB |
Output is correct |
57 |
Correct |
306 ms |
41984 KB |
Output is correct |
58 |
Correct |
351 ms |
38332 KB |
Output is correct |
59 |
Correct |
79 ms |
15896 KB |
Output is correct |
60 |
Correct |
330 ms |
43764 KB |
Output is correct |
61 |
Correct |
327 ms |
42136 KB |
Output is correct |
62 |
Correct |
356 ms |
45244 KB |
Output is correct |
63 |
Correct |
302 ms |
42472 KB |
Output is correct |
64 |
Correct |
3 ms |
5276 KB |
Output is correct |
65 |
Correct |
4 ms |
5460 KB |
Output is correct |
66 |
Correct |
3 ms |
5396 KB |
Output is correct |
67 |
Correct |
3 ms |
5276 KB |
Output is correct |
68 |
Correct |
3 ms |
5332 KB |
Output is correct |
69 |
Correct |
4 ms |
5460 KB |
Output is correct |
70 |
Correct |
4 ms |
5400 KB |
Output is correct |
71 |
Correct |
4 ms |
5400 KB |
Output is correct |
72 |
Correct |
3 ms |
5460 KB |
Output is correct |
73 |
Correct |
4 ms |
5404 KB |
Output is correct |
74 |
Runtime error |
74 ms |
26420 KB |
Execution killed with signal 11 |
75 |
Halted |
0 ms |
0 KB |
- |