#include "swap.h"
#include <iostream>
#include <vector>
#include <algorithm>
#define f first
#define s second
#define int long long
using namespace std;
int n, m, freq[100010], p[100010], id[100010], par[300010][20], depth[300010];
bool can[300010];
pair<int,pair<int,int>> ed[200010];
vector<int> edge[300010];
int dsu(int u) {
if (p[u] == u) return u;
return (p[u] = dsu(p[u]));
}
void dfs(int u, int d) {
int v = par[u][0];
depth[u] = d;
for (int i = 1; i <= 19; i++) {
if (v == -1) {
par[u][i] = -1;
continue;
}
par[u][i] = par[v][i-1];
v = par[u][i];
}
for (int i = 0; i < edge[u].size(); i++) dfs(edge[u][i],d+1);
}
int lca(int u, int v) {
if (depth[v] < depth[u]) swap(u,v);
int i = 19;
while (depth[v] != depth[u]) {
if ((par[v][i] != -1) && (depth[par[v][i]] >= depth[u])) v = par[v][i];
i--;
}
if (v == u) return u;
i = 19;
while (i >= 0) {
if (par[v][i] != par[u][i]) {
u = par[u][i];
v = par[v][i];
}
i--;
}
return par[u][0];
}
void init(signed N, signed M, std::vector<signed> U, std::vector<signed> V, std::vector<signed> W) {
n = N; m = M;
for (int i = 0; i < m; i++) ed[i] = {W[i],{U[i],V[i]}};
sort(ed,ed+m);
for (int i = 0; i < n; i++) p[i] = id[i] = i;
for (int i = 0; i < m; i++) {
int u = ed[i].s.f, v = ed[i].s.s, w = ed[i].f;
if (dsu(u) == dsu(v)) {
par[id[dsu(u)]][0] = n+i;
edge[n+i].push_back(id[dsu(u)]);
id[dsu(u)] = n+i;
freq[u]++;
freq[v]++;
can[n+i] = 1;
} else {
par[id[dsu(u)]][0] = n+i;
par[id[dsu(v)]][0] = n+i;
edge[n+i].push_back(id[dsu(u)]);
edge[n+i].push_back(id[dsu(v)]);
freq[u]++;
freq[v]++;
if ((freq[u] >= 3) || (freq[v] >= 3) || (can[id[dsu(u)]]) || (can[id[dsu(v)]])) can[n+i] = 1;
p[dsu(v)] = p[dsu(u)];
id[dsu(u)] = n+i;
}
}
for (int i = 0; i <= 19; i++) par[n+m-1][i] = -1;
dfs(n+m-1,1);
/*for (int i = 0; i < n+m; i++) {
for (int j = 0; j <= 19; j++) cout << par[i][j] << " ";
cout << endl;
}*/
}
signed getMinimumFuelCapacity(signed U, signed V) {
int u = lca(U,V);
if (can[u]) return ed[u-n].f;
int i = 19;
while (i >= 0) {
if ((par[u][i] != -1) && (!can[par[u][i]])) u = par[u][i];
i--;
}
if (par[u][0] == -1) return -1;
return ed[par[u][0]-n].f;
}
Compilation message
swap.cpp: In function 'void dfs(long long int, long long int)':
swap.cpp:31:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < edge[u].size(); i++) dfs(edge[u][i],d+1);
| ~~^~~~~~~~~~~~~~~~
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:59:43: warning: unused variable 'w' [-Wunused-variable]
59 | int u = ed[i].s.f, v = ed[i].s.s, w = ed[i].f;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7488 KB |
Output is correct |
5 |
Correct |
5 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7764 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
7776 KB |
Output is correct |
9 |
Correct |
104 ms |
41160 KB |
Output is correct |
10 |
Correct |
117 ms |
48716 KB |
Output is correct |
11 |
Correct |
113 ms |
48140 KB |
Output is correct |
12 |
Correct |
118 ms |
50324 KB |
Output is correct |
13 |
Correct |
132 ms |
52680 KB |
Output is correct |
14 |
Correct |
105 ms |
41216 KB |
Output is correct |
15 |
Correct |
274 ms |
50900 KB |
Output is correct |
16 |
Correct |
256 ms |
49608 KB |
Output is correct |
17 |
Correct |
269 ms |
52156 KB |
Output is correct |
18 |
Correct |
431 ms |
54444 KB |
Output is correct |
19 |
Correct |
90 ms |
17596 KB |
Output is correct |
20 |
Correct |
247 ms |
52164 KB |
Output is correct |
21 |
Correct |
253 ms |
50652 KB |
Output is correct |
22 |
Correct |
262 ms |
53568 KB |
Output is correct |
23 |
Correct |
453 ms |
55868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
359 ms |
55564 KB |
Output is correct |
4 |
Correct |
381 ms |
57904 KB |
Output is correct |
5 |
Correct |
372 ms |
56524 KB |
Output is correct |
6 |
Correct |
400 ms |
57812 KB |
Output is correct |
7 |
Correct |
371 ms |
57232 KB |
Output is correct |
8 |
Correct |
356 ms |
55212 KB |
Output is correct |
9 |
Correct |
351 ms |
56824 KB |
Output is correct |
10 |
Correct |
362 ms |
54816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7488 KB |
Output is correct |
5 |
Correct |
5 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7764 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
7776 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7764 KB |
Output is correct |
11 |
Correct |
5 ms |
7752 KB |
Output is correct |
12 |
Correct |
5 ms |
7724 KB |
Output is correct |
13 |
Correct |
5 ms |
7636 KB |
Output is correct |
14 |
Correct |
5 ms |
7764 KB |
Output is correct |
15 |
Correct |
5 ms |
7752 KB |
Output is correct |
16 |
Correct |
4 ms |
7764 KB |
Output is correct |
17 |
Correct |
4 ms |
7856 KB |
Output is correct |
18 |
Correct |
5 ms |
7764 KB |
Output is correct |
19 |
Correct |
5 ms |
7764 KB |
Output is correct |
20 |
Correct |
6 ms |
7748 KB |
Output is correct |
21 |
Correct |
5 ms |
7764 KB |
Output is correct |
22 |
Correct |
7 ms |
7892 KB |
Output is correct |
23 |
Correct |
5 ms |
7636 KB |
Output is correct |
24 |
Correct |
6 ms |
8000 KB |
Output is correct |
25 |
Correct |
5 ms |
8020 KB |
Output is correct |
26 |
Correct |
5 ms |
8020 KB |
Output is correct |
27 |
Correct |
4 ms |
7756 KB |
Output is correct |
28 |
Correct |
5 ms |
8020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7488 KB |
Output is correct |
6 |
Correct |
5 ms |
7764 KB |
Output is correct |
7 |
Correct |
4 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
7764 KB |
Output is correct |
9 |
Correct |
5 ms |
7776 KB |
Output is correct |
10 |
Correct |
104 ms |
41160 KB |
Output is correct |
11 |
Correct |
117 ms |
48716 KB |
Output is correct |
12 |
Correct |
113 ms |
48140 KB |
Output is correct |
13 |
Correct |
118 ms |
50324 KB |
Output is correct |
14 |
Correct |
132 ms |
52680 KB |
Output is correct |
15 |
Correct |
4 ms |
7764 KB |
Output is correct |
16 |
Correct |
5 ms |
7752 KB |
Output is correct |
17 |
Correct |
5 ms |
7724 KB |
Output is correct |
18 |
Correct |
5 ms |
7636 KB |
Output is correct |
19 |
Correct |
5 ms |
7764 KB |
Output is correct |
20 |
Correct |
5 ms |
7752 KB |
Output is correct |
21 |
Correct |
4 ms |
7764 KB |
Output is correct |
22 |
Correct |
4 ms |
7856 KB |
Output is correct |
23 |
Correct |
5 ms |
7764 KB |
Output is correct |
24 |
Correct |
5 ms |
7764 KB |
Output is correct |
25 |
Correct |
6 ms |
7748 KB |
Output is correct |
26 |
Correct |
5 ms |
7764 KB |
Output is correct |
27 |
Correct |
7 ms |
7892 KB |
Output is correct |
28 |
Correct |
5 ms |
7636 KB |
Output is correct |
29 |
Correct |
6 ms |
8000 KB |
Output is correct |
30 |
Correct |
5 ms |
8020 KB |
Output is correct |
31 |
Correct |
5 ms |
8020 KB |
Output is correct |
32 |
Correct |
4 ms |
7756 KB |
Output is correct |
33 |
Correct |
5 ms |
8020 KB |
Output is correct |
34 |
Correct |
17 ms |
13460 KB |
Output is correct |
35 |
Correct |
124 ms |
52276 KB |
Output is correct |
36 |
Correct |
115 ms |
52164 KB |
Output is correct |
37 |
Correct |
113 ms |
52272 KB |
Output is correct |
38 |
Correct |
112 ms |
51640 KB |
Output is correct |
39 |
Correct |
114 ms |
51380 KB |
Output is correct |
40 |
Correct |
115 ms |
47864 KB |
Output is correct |
41 |
Correct |
114 ms |
52440 KB |
Output is correct |
42 |
Correct |
115 ms |
52172 KB |
Output is correct |
43 |
Correct |
136 ms |
55372 KB |
Output is correct |
44 |
Correct |
129 ms |
52676 KB |
Output is correct |
45 |
Correct |
165 ms |
65392 KB |
Output is correct |
46 |
Correct |
116 ms |
52296 KB |
Output is correct |
47 |
Correct |
137 ms |
52416 KB |
Output is correct |
48 |
Correct |
151 ms |
56528 KB |
Output is correct |
49 |
Correct |
100 ms |
56056 KB |
Output is correct |
50 |
Correct |
78 ms |
45868 KB |
Output is correct |
51 |
Correct |
134 ms |
57248 KB |
Output is correct |
52 |
Correct |
181 ms |
71800 KB |
Output is correct |
53 |
Correct |
199 ms |
76124 KB |
Output is correct |
54 |
Correct |
200 ms |
83416 KB |
Output is correct |
55 |
Correct |
122 ms |
55240 KB |
Output is correct |
56 |
Correct |
192 ms |
76364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7488 KB |
Output is correct |
5 |
Correct |
5 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7764 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
7776 KB |
Output is correct |
9 |
Correct |
104 ms |
41160 KB |
Output is correct |
10 |
Correct |
117 ms |
48716 KB |
Output is correct |
11 |
Correct |
113 ms |
48140 KB |
Output is correct |
12 |
Correct |
118 ms |
50324 KB |
Output is correct |
13 |
Correct |
132 ms |
52680 KB |
Output is correct |
14 |
Correct |
105 ms |
41216 KB |
Output is correct |
15 |
Correct |
274 ms |
50900 KB |
Output is correct |
16 |
Correct |
256 ms |
49608 KB |
Output is correct |
17 |
Correct |
269 ms |
52156 KB |
Output is correct |
18 |
Correct |
431 ms |
54444 KB |
Output is correct |
19 |
Correct |
359 ms |
55564 KB |
Output is correct |
20 |
Correct |
381 ms |
57904 KB |
Output is correct |
21 |
Correct |
372 ms |
56524 KB |
Output is correct |
22 |
Correct |
400 ms |
57812 KB |
Output is correct |
23 |
Correct |
371 ms |
57232 KB |
Output is correct |
24 |
Correct |
356 ms |
55212 KB |
Output is correct |
25 |
Correct |
351 ms |
56824 KB |
Output is correct |
26 |
Correct |
362 ms |
54816 KB |
Output is correct |
27 |
Correct |
4 ms |
7764 KB |
Output is correct |
28 |
Correct |
5 ms |
7752 KB |
Output is correct |
29 |
Correct |
5 ms |
7724 KB |
Output is correct |
30 |
Correct |
5 ms |
7636 KB |
Output is correct |
31 |
Correct |
5 ms |
7764 KB |
Output is correct |
32 |
Correct |
5 ms |
7752 KB |
Output is correct |
33 |
Correct |
4 ms |
7764 KB |
Output is correct |
34 |
Correct |
4 ms |
7856 KB |
Output is correct |
35 |
Correct |
5 ms |
7764 KB |
Output is correct |
36 |
Correct |
17 ms |
13460 KB |
Output is correct |
37 |
Correct |
124 ms |
52276 KB |
Output is correct |
38 |
Correct |
115 ms |
52164 KB |
Output is correct |
39 |
Correct |
113 ms |
52272 KB |
Output is correct |
40 |
Correct |
112 ms |
51640 KB |
Output is correct |
41 |
Correct |
114 ms |
51380 KB |
Output is correct |
42 |
Correct |
115 ms |
47864 KB |
Output is correct |
43 |
Correct |
114 ms |
52440 KB |
Output is correct |
44 |
Correct |
115 ms |
52172 KB |
Output is correct |
45 |
Correct |
136 ms |
55372 KB |
Output is correct |
46 |
Correct |
129 ms |
52676 KB |
Output is correct |
47 |
Correct |
23 ms |
13504 KB |
Output is correct |
48 |
Correct |
252 ms |
57140 KB |
Output is correct |
49 |
Correct |
255 ms |
57136 KB |
Output is correct |
50 |
Correct |
241 ms |
57008 KB |
Output is correct |
51 |
Correct |
238 ms |
56784 KB |
Output is correct |
52 |
Correct |
236 ms |
54216 KB |
Output is correct |
53 |
Correct |
205 ms |
43180 KB |
Output is correct |
54 |
Correct |
254 ms |
58036 KB |
Output is correct |
55 |
Correct |
287 ms |
57136 KB |
Output is correct |
56 |
Correct |
413 ms |
59608 KB |
Output is correct |
57 |
Correct |
271 ms |
58168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7488 KB |
Output is correct |
6 |
Correct |
5 ms |
7764 KB |
Output is correct |
7 |
Correct |
4 ms |
7764 KB |
Output is correct |
8 |
Correct |
5 ms |
7764 KB |
Output is correct |
9 |
Correct |
5 ms |
7776 KB |
Output is correct |
10 |
Correct |
104 ms |
41160 KB |
Output is correct |
11 |
Correct |
117 ms |
48716 KB |
Output is correct |
12 |
Correct |
113 ms |
48140 KB |
Output is correct |
13 |
Correct |
118 ms |
50324 KB |
Output is correct |
14 |
Correct |
132 ms |
52680 KB |
Output is correct |
15 |
Correct |
105 ms |
41216 KB |
Output is correct |
16 |
Correct |
274 ms |
50900 KB |
Output is correct |
17 |
Correct |
256 ms |
49608 KB |
Output is correct |
18 |
Correct |
269 ms |
52156 KB |
Output is correct |
19 |
Correct |
431 ms |
54444 KB |
Output is correct |
20 |
Correct |
359 ms |
55564 KB |
Output is correct |
21 |
Correct |
381 ms |
57904 KB |
Output is correct |
22 |
Correct |
372 ms |
56524 KB |
Output is correct |
23 |
Correct |
400 ms |
57812 KB |
Output is correct |
24 |
Correct |
371 ms |
57232 KB |
Output is correct |
25 |
Correct |
356 ms |
55212 KB |
Output is correct |
26 |
Correct |
351 ms |
56824 KB |
Output is correct |
27 |
Correct |
362 ms |
54816 KB |
Output is correct |
28 |
Correct |
4 ms |
7764 KB |
Output is correct |
29 |
Correct |
5 ms |
7752 KB |
Output is correct |
30 |
Correct |
5 ms |
7724 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7764 KB |
Output is correct |
33 |
Correct |
5 ms |
7752 KB |
Output is correct |
34 |
Correct |
4 ms |
7764 KB |
Output is correct |
35 |
Correct |
4 ms |
7856 KB |
Output is correct |
36 |
Correct |
5 ms |
7764 KB |
Output is correct |
37 |
Correct |
17 ms |
13460 KB |
Output is correct |
38 |
Correct |
124 ms |
52276 KB |
Output is correct |
39 |
Correct |
115 ms |
52164 KB |
Output is correct |
40 |
Correct |
113 ms |
52272 KB |
Output is correct |
41 |
Correct |
112 ms |
51640 KB |
Output is correct |
42 |
Correct |
114 ms |
51380 KB |
Output is correct |
43 |
Correct |
115 ms |
47864 KB |
Output is correct |
44 |
Correct |
114 ms |
52440 KB |
Output is correct |
45 |
Correct |
115 ms |
52172 KB |
Output is correct |
46 |
Correct |
136 ms |
55372 KB |
Output is correct |
47 |
Correct |
129 ms |
52676 KB |
Output is correct |
48 |
Correct |
23 ms |
13504 KB |
Output is correct |
49 |
Correct |
252 ms |
57140 KB |
Output is correct |
50 |
Correct |
255 ms |
57136 KB |
Output is correct |
51 |
Correct |
241 ms |
57008 KB |
Output is correct |
52 |
Correct |
238 ms |
56784 KB |
Output is correct |
53 |
Correct |
236 ms |
54216 KB |
Output is correct |
54 |
Correct |
205 ms |
43180 KB |
Output is correct |
55 |
Correct |
254 ms |
58036 KB |
Output is correct |
56 |
Correct |
287 ms |
57136 KB |
Output is correct |
57 |
Correct |
413 ms |
59608 KB |
Output is correct |
58 |
Correct |
271 ms |
58168 KB |
Output is correct |
59 |
Correct |
90 ms |
17596 KB |
Output is correct |
60 |
Correct |
247 ms |
52164 KB |
Output is correct |
61 |
Correct |
253 ms |
50652 KB |
Output is correct |
62 |
Correct |
262 ms |
53568 KB |
Output is correct |
63 |
Correct |
453 ms |
55868 KB |
Output is correct |
64 |
Correct |
5 ms |
7764 KB |
Output is correct |
65 |
Correct |
6 ms |
7748 KB |
Output is correct |
66 |
Correct |
5 ms |
7764 KB |
Output is correct |
67 |
Correct |
7 ms |
7892 KB |
Output is correct |
68 |
Correct |
5 ms |
7636 KB |
Output is correct |
69 |
Correct |
6 ms |
8000 KB |
Output is correct |
70 |
Correct |
5 ms |
8020 KB |
Output is correct |
71 |
Correct |
5 ms |
8020 KB |
Output is correct |
72 |
Correct |
4 ms |
7756 KB |
Output is correct |
73 |
Correct |
5 ms |
8020 KB |
Output is correct |
74 |
Correct |
165 ms |
65392 KB |
Output is correct |
75 |
Correct |
116 ms |
52296 KB |
Output is correct |
76 |
Correct |
137 ms |
52416 KB |
Output is correct |
77 |
Correct |
151 ms |
56528 KB |
Output is correct |
78 |
Correct |
100 ms |
56056 KB |
Output is correct |
79 |
Correct |
78 ms |
45868 KB |
Output is correct |
80 |
Correct |
134 ms |
57248 KB |
Output is correct |
81 |
Correct |
181 ms |
71800 KB |
Output is correct |
82 |
Correct |
199 ms |
76124 KB |
Output is correct |
83 |
Correct |
200 ms |
83416 KB |
Output is correct |
84 |
Correct |
122 ms |
55240 KB |
Output is correct |
85 |
Correct |
192 ms |
76364 KB |
Output is correct |
86 |
Correct |
100 ms |
28848 KB |
Output is correct |
87 |
Correct |
241 ms |
57216 KB |
Output is correct |
88 |
Correct |
294 ms |
57168 KB |
Output is correct |
89 |
Correct |
367 ms |
58888 KB |
Output is correct |
90 |
Correct |
196 ms |
62968 KB |
Output is correct |
91 |
Correct |
212 ms |
59560 KB |
Output is correct |
92 |
Correct |
364 ms |
60460 KB |
Output is correct |
93 |
Correct |
313 ms |
75496 KB |
Output is correct |
94 |
Correct |
419 ms |
80428 KB |
Output is correct |
95 |
Correct |
352 ms |
87244 KB |
Output is correct |
96 |
Correct |
431 ms |
59848 KB |
Output is correct |
97 |
Correct |
424 ms |
69924 KB |
Output is correct |