#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> II;
const int maxn = 3e5 + 5, logN = 20;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
struct edge{
int u, v, w;
edge(int _u = 0, int _v = 0, int _w = 0) : u(_u), v(_v), w(_w) {}
bool operator < (const edge &op) const{
return w < op.w;
}
}ed[maxn];
int n, m, par[maxn], f[maxn][logN], up[maxn], L[maxn], R[maxn], depth[maxn], val[maxn];
vector<int> g[maxn];
bool isline[maxn];
int get(int u){
if(par[u] != u) par[u] = get(par[u]);
return par[u];
}
void dfs(int u, int p){
up[u] = ((isline[u] == false) ? u : up[p]);
depth[u] = depth[p] + 1;
f[u][0] = p;
for(int i = 1; i < logN; i ++) f[u][i] = f[f[u][i - 1]][i - 1];
for(int v : g[u]){
dfs(v, u);
}
}
int lca(int u, int v){
if(depth[u] < depth[v]) swap(u, v);
for(int i = logN - 1; i >= 0; i --){
if(depth[f[u][i]] >= depth[v]) u = f[u][i];
}
if(u == v) return u;
for(int i = logN - 1; i >= 0; i --){
if(f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
}
return f[u][0];
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){
n = N, m = M;
for(int i = 1; i <= m; i ++) ed[i] = edge(U[i - 1] + 1, V[i - 1] + 1, W[i - 1]);
sort(ed + 1, ed + m + 1);
for(int i = 1; i <= n; i ++) L[i] = R[i] = i, isline[i] = true;
for(int i = 1; i <= n + m; i ++) par[i] = i;
for(int i = 1; i <= m; i ++){
int u = ed[i].u, v = ed[i].v, w = ed[i].w;
u = get(u), v = get(v);
par[u] = par[v] = i + n;
val[i + n] = w;
isline[i + n] = (isline[u] && isline[v] && (u != v));
bool flag = false;
if(isline[i + n]){
bool flag = false;
for(int x : {L[u], R[u]}){
for(int y : {L[v], R[v]}){
if((x == ed[i].u && y == ed[i].v) || (x == ed[i].v && y == ed[i].u)){
L[i + n] = L[u] ^ R[u] ^ x;
R[i + n] = L[v] ^ R[v] ^ y;
flag = true;
break;
}
}
}
isline[i + n] &= flag;
}
g[i + n].push_back(u);
if(u != v) g[i + n].push_back(v);
}
dfs(n + m, 0);
}
int getMinimumFuelCapacity(int X, int Y){
if(isline[n + m]) return -1;
return val[up[lca(X + 1, Y + 1)]];
}
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:60:20: warning: unused variable 'flag' [-Wunused-variable]
60 | bool flag = false;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10836 KB |
Output is correct |
2 |
Correct |
5 ms |
10836 KB |
Output is correct |
3 |
Correct |
5 ms |
10836 KB |
Output is correct |
4 |
Correct |
6 ms |
10964 KB |
Output is correct |
5 |
Correct |
6 ms |
11092 KB |
Output is correct |
6 |
Correct |
6 ms |
11092 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
95 ms |
31012 KB |
Output is correct |
10 |
Correct |
116 ms |
35440 KB |
Output is correct |
11 |
Correct |
113 ms |
34996 KB |
Output is correct |
12 |
Correct |
124 ms |
36572 KB |
Output is correct |
13 |
Correct |
108 ms |
38044 KB |
Output is correct |
14 |
Correct |
104 ms |
31140 KB |
Output is correct |
15 |
Correct |
166 ms |
37512 KB |
Output is correct |
16 |
Correct |
161 ms |
36860 KB |
Output is correct |
17 |
Correct |
169 ms |
38304 KB |
Output is correct |
18 |
Correct |
149 ms |
39820 KB |
Output is correct |
19 |
Correct |
96 ms |
18404 KB |
Output is correct |
20 |
Correct |
246 ms |
38728 KB |
Output is correct |
21 |
Correct |
248 ms |
38056 KB |
Output is correct |
22 |
Correct |
275 ms |
39704 KB |
Output is correct |
23 |
Correct |
381 ms |
41208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10836 KB |
Output is correct |
2 |
Correct |
5 ms |
10836 KB |
Output is correct |
3 |
Correct |
302 ms |
40336 KB |
Output is correct |
4 |
Correct |
294 ms |
45612 KB |
Output is correct |
5 |
Correct |
308 ms |
44912 KB |
Output is correct |
6 |
Correct |
286 ms |
45308 KB |
Output is correct |
7 |
Correct |
299 ms |
45284 KB |
Output is correct |
8 |
Correct |
289 ms |
44064 KB |
Output is correct |
9 |
Correct |
280 ms |
45032 KB |
Output is correct |
10 |
Correct |
287 ms |
43804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10836 KB |
Output is correct |
2 |
Correct |
5 ms |
10836 KB |
Output is correct |
3 |
Correct |
5 ms |
10836 KB |
Output is correct |
4 |
Correct |
6 ms |
10964 KB |
Output is correct |
5 |
Correct |
6 ms |
11092 KB |
Output is correct |
6 |
Correct |
6 ms |
11092 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
5 ms |
10836 KB |
Output is correct |
10 |
Correct |
6 ms |
11092 KB |
Output is correct |
11 |
Correct |
6 ms |
11152 KB |
Output is correct |
12 |
Correct |
7 ms |
11092 KB |
Output is correct |
13 |
Correct |
7 ms |
11092 KB |
Output is correct |
14 |
Correct |
6 ms |
11156 KB |
Output is correct |
15 |
Correct |
7 ms |
11092 KB |
Output is correct |
16 |
Correct |
6 ms |
11156 KB |
Output is correct |
17 |
Correct |
6 ms |
11112 KB |
Output is correct |
18 |
Correct |
6 ms |
11156 KB |
Output is correct |
19 |
Correct |
6 ms |
11092 KB |
Output is correct |
20 |
Correct |
6 ms |
11124 KB |
Output is correct |
21 |
Correct |
6 ms |
11156 KB |
Output is correct |
22 |
Correct |
6 ms |
11192 KB |
Output is correct |
23 |
Correct |
6 ms |
11104 KB |
Output is correct |
24 |
Correct |
7 ms |
11348 KB |
Output is correct |
25 |
Correct |
8 ms |
11256 KB |
Output is correct |
26 |
Correct |
7 ms |
11348 KB |
Output is correct |
27 |
Correct |
6 ms |
11092 KB |
Output is correct |
28 |
Correct |
8 ms |
11324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10836 KB |
Output is correct |
2 |
Correct |
5 ms |
10836 KB |
Output is correct |
3 |
Correct |
5 ms |
10836 KB |
Output is correct |
4 |
Correct |
5 ms |
10836 KB |
Output is correct |
5 |
Correct |
6 ms |
10964 KB |
Output is correct |
6 |
Correct |
6 ms |
11092 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
6 ms |
11092 KB |
Output is correct |
10 |
Correct |
95 ms |
31012 KB |
Output is correct |
11 |
Correct |
116 ms |
35440 KB |
Output is correct |
12 |
Correct |
113 ms |
34996 KB |
Output is correct |
13 |
Correct |
124 ms |
36572 KB |
Output is correct |
14 |
Correct |
108 ms |
38044 KB |
Output is correct |
15 |
Correct |
6 ms |
11092 KB |
Output is correct |
16 |
Correct |
6 ms |
11152 KB |
Output is correct |
17 |
Correct |
7 ms |
11092 KB |
Output is correct |
18 |
Correct |
7 ms |
11092 KB |
Output is correct |
19 |
Correct |
6 ms |
11156 KB |
Output is correct |
20 |
Correct |
7 ms |
11092 KB |
Output is correct |
21 |
Correct |
6 ms |
11156 KB |
Output is correct |
22 |
Correct |
6 ms |
11112 KB |
Output is correct |
23 |
Correct |
6 ms |
11156 KB |
Output is correct |
24 |
Correct |
6 ms |
11092 KB |
Output is correct |
25 |
Correct |
6 ms |
11124 KB |
Output is correct |
26 |
Correct |
6 ms |
11156 KB |
Output is correct |
27 |
Correct |
6 ms |
11192 KB |
Output is correct |
28 |
Correct |
6 ms |
11104 KB |
Output is correct |
29 |
Correct |
7 ms |
11348 KB |
Output is correct |
30 |
Correct |
8 ms |
11256 KB |
Output is correct |
31 |
Correct |
7 ms |
11348 KB |
Output is correct |
32 |
Correct |
6 ms |
11092 KB |
Output is correct |
33 |
Correct |
8 ms |
11324 KB |
Output is correct |
34 |
Correct |
18 ms |
14648 KB |
Output is correct |
35 |
Correct |
123 ms |
38396 KB |
Output is correct |
36 |
Correct |
118 ms |
38532 KB |
Output is correct |
37 |
Correct |
124 ms |
38352 KB |
Output is correct |
38 |
Correct |
121 ms |
37888 KB |
Output is correct |
39 |
Correct |
117 ms |
37836 KB |
Output is correct |
40 |
Correct |
105 ms |
35600 KB |
Output is correct |
41 |
Correct |
122 ms |
38616 KB |
Output is correct |
42 |
Correct |
118 ms |
38296 KB |
Output is correct |
43 |
Correct |
104 ms |
40424 KB |
Output is correct |
44 |
Correct |
125 ms |
38668 KB |
Output is correct |
45 |
Correct |
156 ms |
47668 KB |
Output is correct |
46 |
Correct |
128 ms |
38312 KB |
Output is correct |
47 |
Correct |
123 ms |
38556 KB |
Output is correct |
48 |
Correct |
126 ms |
41232 KB |
Output is correct |
49 |
Correct |
98 ms |
42296 KB |
Output is correct |
50 |
Correct |
82 ms |
35740 KB |
Output is correct |
51 |
Correct |
121 ms |
42472 KB |
Output is correct |
52 |
Correct |
164 ms |
51048 KB |
Output is correct |
53 |
Correct |
186 ms |
53836 KB |
Output is correct |
54 |
Correct |
196 ms |
58536 KB |
Output is correct |
55 |
Correct |
96 ms |
40248 KB |
Output is correct |
56 |
Correct |
178 ms |
54656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10836 KB |
Output is correct |
2 |
Correct |
5 ms |
10836 KB |
Output is correct |
3 |
Correct |
5 ms |
10836 KB |
Output is correct |
4 |
Correct |
6 ms |
10964 KB |
Output is correct |
5 |
Correct |
6 ms |
11092 KB |
Output is correct |
6 |
Correct |
6 ms |
11092 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
95 ms |
31012 KB |
Output is correct |
10 |
Correct |
116 ms |
35440 KB |
Output is correct |
11 |
Correct |
113 ms |
34996 KB |
Output is correct |
12 |
Correct |
124 ms |
36572 KB |
Output is correct |
13 |
Correct |
108 ms |
38044 KB |
Output is correct |
14 |
Correct |
104 ms |
31140 KB |
Output is correct |
15 |
Correct |
166 ms |
37512 KB |
Output is correct |
16 |
Correct |
161 ms |
36860 KB |
Output is correct |
17 |
Correct |
169 ms |
38304 KB |
Output is correct |
18 |
Correct |
149 ms |
39820 KB |
Output is correct |
19 |
Correct |
302 ms |
40336 KB |
Output is correct |
20 |
Correct |
294 ms |
45612 KB |
Output is correct |
21 |
Correct |
308 ms |
44912 KB |
Output is correct |
22 |
Correct |
286 ms |
45308 KB |
Output is correct |
23 |
Correct |
299 ms |
45284 KB |
Output is correct |
24 |
Correct |
289 ms |
44064 KB |
Output is correct |
25 |
Correct |
280 ms |
45032 KB |
Output is correct |
26 |
Correct |
287 ms |
43804 KB |
Output is correct |
27 |
Correct |
6 ms |
11092 KB |
Output is correct |
28 |
Correct |
6 ms |
11152 KB |
Output is correct |
29 |
Correct |
7 ms |
11092 KB |
Output is correct |
30 |
Correct |
7 ms |
11092 KB |
Output is correct |
31 |
Correct |
6 ms |
11156 KB |
Output is correct |
32 |
Correct |
7 ms |
11092 KB |
Output is correct |
33 |
Correct |
6 ms |
11156 KB |
Output is correct |
34 |
Correct |
6 ms |
11112 KB |
Output is correct |
35 |
Correct |
6 ms |
11156 KB |
Output is correct |
36 |
Correct |
18 ms |
14648 KB |
Output is correct |
37 |
Correct |
123 ms |
38396 KB |
Output is correct |
38 |
Correct |
118 ms |
38532 KB |
Output is correct |
39 |
Correct |
124 ms |
38352 KB |
Output is correct |
40 |
Correct |
121 ms |
37888 KB |
Output is correct |
41 |
Correct |
117 ms |
37836 KB |
Output is correct |
42 |
Correct |
105 ms |
35600 KB |
Output is correct |
43 |
Correct |
122 ms |
38616 KB |
Output is correct |
44 |
Correct |
118 ms |
38296 KB |
Output is correct |
45 |
Correct |
104 ms |
40424 KB |
Output is correct |
46 |
Correct |
125 ms |
38668 KB |
Output is correct |
47 |
Correct |
26 ms |
14796 KB |
Output is correct |
48 |
Correct |
261 ms |
43268 KB |
Output is correct |
49 |
Correct |
252 ms |
43312 KB |
Output is correct |
50 |
Correct |
240 ms |
43132 KB |
Output is correct |
51 |
Correct |
256 ms |
43064 KB |
Output is correct |
52 |
Correct |
228 ms |
41448 KB |
Output is correct |
53 |
Correct |
198 ms |
35136 KB |
Output is correct |
54 |
Correct |
287 ms |
44080 KB |
Output is correct |
55 |
Correct |
287 ms |
43252 KB |
Output is correct |
56 |
Correct |
321 ms |
44776 KB |
Output is correct |
57 |
Correct |
252 ms |
44208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
10836 KB |
Output is correct |
2 |
Correct |
5 ms |
10836 KB |
Output is correct |
3 |
Correct |
5 ms |
10836 KB |
Output is correct |
4 |
Correct |
5 ms |
10836 KB |
Output is correct |
5 |
Correct |
6 ms |
10964 KB |
Output is correct |
6 |
Correct |
6 ms |
11092 KB |
Output is correct |
7 |
Correct |
6 ms |
11092 KB |
Output is correct |
8 |
Correct |
6 ms |
11092 KB |
Output is correct |
9 |
Correct |
6 ms |
11092 KB |
Output is correct |
10 |
Correct |
95 ms |
31012 KB |
Output is correct |
11 |
Correct |
116 ms |
35440 KB |
Output is correct |
12 |
Correct |
113 ms |
34996 KB |
Output is correct |
13 |
Correct |
124 ms |
36572 KB |
Output is correct |
14 |
Correct |
108 ms |
38044 KB |
Output is correct |
15 |
Correct |
104 ms |
31140 KB |
Output is correct |
16 |
Correct |
166 ms |
37512 KB |
Output is correct |
17 |
Correct |
161 ms |
36860 KB |
Output is correct |
18 |
Correct |
169 ms |
38304 KB |
Output is correct |
19 |
Correct |
149 ms |
39820 KB |
Output is correct |
20 |
Correct |
302 ms |
40336 KB |
Output is correct |
21 |
Correct |
294 ms |
45612 KB |
Output is correct |
22 |
Correct |
308 ms |
44912 KB |
Output is correct |
23 |
Correct |
286 ms |
45308 KB |
Output is correct |
24 |
Correct |
299 ms |
45284 KB |
Output is correct |
25 |
Correct |
289 ms |
44064 KB |
Output is correct |
26 |
Correct |
280 ms |
45032 KB |
Output is correct |
27 |
Correct |
287 ms |
43804 KB |
Output is correct |
28 |
Correct |
6 ms |
11092 KB |
Output is correct |
29 |
Correct |
6 ms |
11152 KB |
Output is correct |
30 |
Correct |
7 ms |
11092 KB |
Output is correct |
31 |
Correct |
7 ms |
11092 KB |
Output is correct |
32 |
Correct |
6 ms |
11156 KB |
Output is correct |
33 |
Correct |
7 ms |
11092 KB |
Output is correct |
34 |
Correct |
6 ms |
11156 KB |
Output is correct |
35 |
Correct |
6 ms |
11112 KB |
Output is correct |
36 |
Correct |
6 ms |
11156 KB |
Output is correct |
37 |
Correct |
18 ms |
14648 KB |
Output is correct |
38 |
Correct |
123 ms |
38396 KB |
Output is correct |
39 |
Correct |
118 ms |
38532 KB |
Output is correct |
40 |
Correct |
124 ms |
38352 KB |
Output is correct |
41 |
Correct |
121 ms |
37888 KB |
Output is correct |
42 |
Correct |
117 ms |
37836 KB |
Output is correct |
43 |
Correct |
105 ms |
35600 KB |
Output is correct |
44 |
Correct |
122 ms |
38616 KB |
Output is correct |
45 |
Correct |
118 ms |
38296 KB |
Output is correct |
46 |
Correct |
104 ms |
40424 KB |
Output is correct |
47 |
Correct |
125 ms |
38668 KB |
Output is correct |
48 |
Correct |
26 ms |
14796 KB |
Output is correct |
49 |
Correct |
261 ms |
43268 KB |
Output is correct |
50 |
Correct |
252 ms |
43312 KB |
Output is correct |
51 |
Correct |
240 ms |
43132 KB |
Output is correct |
52 |
Correct |
256 ms |
43064 KB |
Output is correct |
53 |
Correct |
228 ms |
41448 KB |
Output is correct |
54 |
Correct |
198 ms |
35136 KB |
Output is correct |
55 |
Correct |
287 ms |
44080 KB |
Output is correct |
56 |
Correct |
287 ms |
43252 KB |
Output is correct |
57 |
Correct |
321 ms |
44776 KB |
Output is correct |
58 |
Correct |
252 ms |
44208 KB |
Output is correct |
59 |
Correct |
96 ms |
18404 KB |
Output is correct |
60 |
Correct |
246 ms |
38728 KB |
Output is correct |
61 |
Correct |
248 ms |
38056 KB |
Output is correct |
62 |
Correct |
275 ms |
39704 KB |
Output is correct |
63 |
Correct |
381 ms |
41208 KB |
Output is correct |
64 |
Correct |
6 ms |
11092 KB |
Output is correct |
65 |
Correct |
6 ms |
11124 KB |
Output is correct |
66 |
Correct |
6 ms |
11156 KB |
Output is correct |
67 |
Correct |
6 ms |
11192 KB |
Output is correct |
68 |
Correct |
6 ms |
11104 KB |
Output is correct |
69 |
Correct |
7 ms |
11348 KB |
Output is correct |
70 |
Correct |
8 ms |
11256 KB |
Output is correct |
71 |
Correct |
7 ms |
11348 KB |
Output is correct |
72 |
Correct |
6 ms |
11092 KB |
Output is correct |
73 |
Correct |
8 ms |
11324 KB |
Output is correct |
74 |
Correct |
156 ms |
47668 KB |
Output is correct |
75 |
Correct |
128 ms |
38312 KB |
Output is correct |
76 |
Correct |
123 ms |
38556 KB |
Output is correct |
77 |
Correct |
126 ms |
41232 KB |
Output is correct |
78 |
Correct |
98 ms |
42296 KB |
Output is correct |
79 |
Correct |
82 ms |
35740 KB |
Output is correct |
80 |
Correct |
121 ms |
42472 KB |
Output is correct |
81 |
Correct |
164 ms |
51048 KB |
Output is correct |
82 |
Correct |
186 ms |
53836 KB |
Output is correct |
83 |
Correct |
196 ms |
58536 KB |
Output is correct |
84 |
Correct |
96 ms |
40248 KB |
Output is correct |
85 |
Correct |
178 ms |
54656 KB |
Output is correct |
86 |
Correct |
89 ms |
24760 KB |
Output is correct |
87 |
Correct |
249 ms |
43260 KB |
Output is correct |
88 |
Correct |
258 ms |
43240 KB |
Output is correct |
89 |
Correct |
292 ms |
44624 KB |
Output is correct |
90 |
Correct |
212 ms |
48228 KB |
Output is correct |
91 |
Correct |
221 ms |
46192 KB |
Output is correct |
92 |
Correct |
306 ms |
46584 KB |
Output is correct |
93 |
Correct |
292 ms |
55172 KB |
Output is correct |
94 |
Correct |
378 ms |
58316 KB |
Output is correct |
95 |
Correct |
342 ms |
62576 KB |
Output is correct |
96 |
Correct |
340 ms |
45040 KB |
Output is correct |
97 |
Correct |
340 ms |
51952 KB |
Output is correct |