#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 100;
const int INF = 1e7 + 10;
long long n,m,q,dsz,E;
bool invalid;
int timer = 1;
vector<int>adj[N + 1];
int fa[N + 1];
bool cyc[N + 1];
int up[23][N + 1],dp[N + 1],val[N + 1];
int lf[N + 1],ri[N + 1];
int in[N + 1],to[N + 1];
int deg[N + 1],deg1[N + 1],deg3[N + 1];
vector<pair<int,pair<int,int>>>edges;
int find(int u){
if(u == fa[u]) return u;
return fa[u] = find(fa[u]);
}
void dfs_calc(int u){
if(!val[u]) val[u] = val[up[0][u]];
for(int i = 1; i < 23 ; i++){
up[i][u] = up[i - 1][up[i - 1][u]];
}
for(int i = 0; i < adj[u].size(); i++){
int v = adj[u][i];
if(v == up[0][u]) continue;
dp[v] = dp[up[0][v] = u] + 1;
dfs_calc(v);
}
}
int lca(int a, int b){
if(dp[a] < dp[b]) swap(a,b);
int d = dp[a] - dp[b];
for(int i = 0; i < 23 ; i++){
if( (d >> i) & 1) a = up[i][a];
}
if(a == b) return a;
for(int i = 22; i >= 0; i--){
int tA = up[i][a]; int tB = up[i][b];
if(tA != tB) {
//cout<<tA<<' '<<tB<<' '<<i<<endl;
a = tA; b = tB;
}
}
// cout<<a<<' '<<b<<endl;
return up[0][a];
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W){
for(int i = 0; i < M; i++){
U[i]++; V[i]++;
edges.push_back({W[i],{U[i],V[i]}});
// cout<<U[i]<<' '<<V[i]<<' '<<W[i]<<endl;
}
sort(edges.begin(),edges.end());
E = N;
for(int i = 0; i <= N; i++) fa[i] = i;
for(int i = 0; i < M; i++){
int u = edges[i].second.first; int v = edges[i].second.second;
int x = find(u); int y = find(v);
deg[u]++; deg[v]++;
if(deg[u] == 2) deg1[x]--; if(deg[v] == 2) deg1[y]--;
if(deg[u] == 1) deg1[x]++; if(deg[v] == 1) deg1[y]++;
if(deg[u] == 3) deg3[x]++; if(deg[v] == 3) deg3[y]++;
E++;
if(x == y){
fa[x] = fa[E] = E;
adj[E].push_back(x);
deg1[E] += deg1[x];
deg3[E] += deg3[y];
}
else{
if(x < y) swap(x,y);
// if(i == 1) cout<<deg1[x]<<' '<<deg1[y]<<endl;
fa[x] = fa[y] = fa[E] = E;
deg1[E] += deg1[x] + deg1[y];
deg3[E] += deg3[x] + deg3[y];
adj[E].push_back(x);
adj[E].push_back(y);
}
if(!deg1[E] or deg3[E]) val[E] = edges[i].first;
}
//cout<<up[0][9]<<endl;
dfs_calc(E);
}
int getMinimumFuelCapacity(int X, int Y){
int anc = lca(++X,++Y);
// cout<<anc<<endl;
return val[anc] == 0 ? -1 : val[anc];
}
Compilation message
swap.cpp: In function 'void dfs_calc(int)':
swap.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < adj[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:63:15: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
63 | if(deg[u] == 2) deg1[x]--; if(deg[v] == 2) deg1[y]--;
| ^~
swap.cpp:63:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
63 | if(deg[u] == 2) deg1[x]--; if(deg[v] == 2) deg1[y]--;
| ^~
swap.cpp:64:15: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
64 | if(deg[u] == 1) deg1[x]++; if(deg[v] == 1) deg1[y]++;
| ^~
swap.cpp:64:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
64 | if(deg[u] == 1) deg1[x]++; if(deg[v] == 1) deg1[y]++;
| ^~
swap.cpp:65:15: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
65 | if(deg[u] == 3) deg3[x]++; if(deg[v] == 3) deg3[y]++;
| ^~
swap.cpp:65:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
65 | if(deg[u] == 3) deg3[x]++; if(deg[v] == 3) deg3[y]++;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
3 ms |
7508 KB |
Output is correct |
4 |
Correct |
4 ms |
7636 KB |
Output is correct |
5 |
Correct |
7 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7752 KB |
Output is correct |
7 |
Correct |
5 ms |
7824 KB |
Output is correct |
8 |
Correct |
6 ms |
7764 KB |
Output is correct |
9 |
Correct |
104 ms |
29888 KB |
Output is correct |
10 |
Correct |
120 ms |
34920 KB |
Output is correct |
11 |
Correct |
119 ms |
34488 KB |
Output is correct |
12 |
Correct |
132 ms |
36068 KB |
Output is correct |
13 |
Correct |
92 ms |
39192 KB |
Output is correct |
14 |
Correct |
121 ms |
29964 KB |
Output is correct |
15 |
Correct |
257 ms |
36940 KB |
Output is correct |
16 |
Correct |
255 ms |
36152 KB |
Output is correct |
17 |
Correct |
269 ms |
37760 KB |
Output is correct |
18 |
Correct |
327 ms |
40888 KB |
Output is correct |
19 |
Correct |
86 ms |
15524 KB |
Output is correct |
20 |
Correct |
256 ms |
38192 KB |
Output is correct |
21 |
Correct |
270 ms |
37272 KB |
Output is correct |
22 |
Correct |
287 ms |
39220 KB |
Output is correct |
23 |
Correct |
316 ms |
42392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
281 ms |
43448 KB |
Output is correct |
4 |
Correct |
286 ms |
48924 KB |
Output is correct |
5 |
Correct |
291 ms |
48088 KB |
Output is correct |
6 |
Correct |
293 ms |
48932 KB |
Output is correct |
7 |
Correct |
293 ms |
48576 KB |
Output is correct |
8 |
Correct |
287 ms |
47032 KB |
Output is correct |
9 |
Correct |
289 ms |
48228 KB |
Output is correct |
10 |
Correct |
295 ms |
46748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
3 ms |
7508 KB |
Output is correct |
4 |
Correct |
4 ms |
7636 KB |
Output is correct |
5 |
Correct |
7 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7752 KB |
Output is correct |
7 |
Correct |
5 ms |
7824 KB |
Output is correct |
8 |
Correct |
6 ms |
7764 KB |
Output is correct |
9 |
Correct |
4 ms |
7508 KB |
Output is correct |
10 |
Correct |
4 ms |
7764 KB |
Output is correct |
11 |
Correct |
5 ms |
7764 KB |
Output is correct |
12 |
Correct |
5 ms |
7764 KB |
Output is correct |
13 |
Correct |
6 ms |
7752 KB |
Output is correct |
14 |
Correct |
5 ms |
7752 KB |
Output is correct |
15 |
Correct |
5 ms |
7756 KB |
Output is correct |
16 |
Correct |
4 ms |
7832 KB |
Output is correct |
17 |
Correct |
4 ms |
7764 KB |
Output is correct |
18 |
Correct |
4 ms |
7760 KB |
Output is correct |
19 |
Correct |
6 ms |
7892 KB |
Output is correct |
20 |
Correct |
5 ms |
7716 KB |
Output is correct |
21 |
Correct |
5 ms |
7752 KB |
Output is correct |
22 |
Correct |
5 ms |
7924 KB |
Output is correct |
23 |
Correct |
4 ms |
7760 KB |
Output is correct |
24 |
Correct |
5 ms |
8020 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 |
7764 KB |
Output is correct |
28 |
Correct |
5 ms |
8020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
3 ms |
7508 KB |
Output is correct |
3 |
Correct |
4 ms |
7508 KB |
Output is correct |
4 |
Correct |
3 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7636 KB |
Output is correct |
6 |
Correct |
7 ms |
7764 KB |
Output is correct |
7 |
Correct |
4 ms |
7752 KB |
Output is correct |
8 |
Correct |
5 ms |
7824 KB |
Output is correct |
9 |
Correct |
6 ms |
7764 KB |
Output is correct |
10 |
Correct |
104 ms |
29888 KB |
Output is correct |
11 |
Correct |
120 ms |
34920 KB |
Output is correct |
12 |
Correct |
119 ms |
34488 KB |
Output is correct |
13 |
Correct |
132 ms |
36068 KB |
Output is correct |
14 |
Correct |
92 ms |
39192 KB |
Output is correct |
15 |
Correct |
4 ms |
7764 KB |
Output is correct |
16 |
Correct |
5 ms |
7764 KB |
Output is correct |
17 |
Correct |
5 ms |
7764 KB |
Output is correct |
18 |
Correct |
6 ms |
7752 KB |
Output is correct |
19 |
Correct |
5 ms |
7752 KB |
Output is correct |
20 |
Correct |
5 ms |
7756 KB |
Output is correct |
21 |
Correct |
4 ms |
7832 KB |
Output is correct |
22 |
Correct |
4 ms |
7764 KB |
Output is correct |
23 |
Correct |
4 ms |
7760 KB |
Output is correct |
24 |
Correct |
6 ms |
7892 KB |
Output is correct |
25 |
Correct |
5 ms |
7716 KB |
Output is correct |
26 |
Correct |
5 ms |
7752 KB |
Output is correct |
27 |
Correct |
5 ms |
7924 KB |
Output is correct |
28 |
Correct |
4 ms |
7760 KB |
Output is correct |
29 |
Correct |
5 ms |
8020 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 |
7764 KB |
Output is correct |
33 |
Correct |
5 ms |
8020 KB |
Output is correct |
34 |
Correct |
15 ms |
11668 KB |
Output is correct |
35 |
Correct |
126 ms |
37896 KB |
Output is correct |
36 |
Correct |
128 ms |
37820 KB |
Output is correct |
37 |
Correct |
131 ms |
37812 KB |
Output is correct |
38 |
Correct |
141 ms |
37416 KB |
Output is correct |
39 |
Correct |
125 ms |
37412 KB |
Output is correct |
40 |
Correct |
116 ms |
34752 KB |
Output is correct |
41 |
Correct |
129 ms |
38068 KB |
Output is correct |
42 |
Correct |
125 ms |
37856 KB |
Output is correct |
43 |
Correct |
95 ms |
41920 KB |
Output is correct |
44 |
Correct |
127 ms |
38164 KB |
Output is correct |
45 |
Correct |
147 ms |
50776 KB |
Output is correct |
46 |
Correct |
135 ms |
37896 KB |
Output is correct |
47 |
Correct |
128 ms |
38024 KB |
Output is correct |
48 |
Correct |
124 ms |
42168 KB |
Output is correct |
49 |
Correct |
99 ms |
47972 KB |
Output is correct |
50 |
Correct |
78 ms |
39104 KB |
Output is correct |
51 |
Correct |
119 ms |
45108 KB |
Output is correct |
52 |
Correct |
177 ms |
54148 KB |
Output is correct |
53 |
Correct |
196 ms |
57464 KB |
Output is correct |
54 |
Correct |
197 ms |
63712 KB |
Output is correct |
55 |
Correct |
93 ms |
41576 KB |
Output is correct |
56 |
Correct |
181 ms |
58716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
3 ms |
7508 KB |
Output is correct |
4 |
Correct |
4 ms |
7636 KB |
Output is correct |
5 |
Correct |
7 ms |
7764 KB |
Output is correct |
6 |
Correct |
4 ms |
7752 KB |
Output is correct |
7 |
Correct |
5 ms |
7824 KB |
Output is correct |
8 |
Correct |
6 ms |
7764 KB |
Output is correct |
9 |
Correct |
104 ms |
29888 KB |
Output is correct |
10 |
Correct |
120 ms |
34920 KB |
Output is correct |
11 |
Correct |
119 ms |
34488 KB |
Output is correct |
12 |
Correct |
132 ms |
36068 KB |
Output is correct |
13 |
Correct |
92 ms |
39192 KB |
Output is correct |
14 |
Correct |
121 ms |
29964 KB |
Output is correct |
15 |
Correct |
257 ms |
36940 KB |
Output is correct |
16 |
Correct |
255 ms |
36152 KB |
Output is correct |
17 |
Correct |
269 ms |
37760 KB |
Output is correct |
18 |
Correct |
327 ms |
40888 KB |
Output is correct |
19 |
Correct |
281 ms |
43448 KB |
Output is correct |
20 |
Correct |
286 ms |
48924 KB |
Output is correct |
21 |
Correct |
291 ms |
48088 KB |
Output is correct |
22 |
Correct |
293 ms |
48932 KB |
Output is correct |
23 |
Correct |
293 ms |
48576 KB |
Output is correct |
24 |
Correct |
287 ms |
47032 KB |
Output is correct |
25 |
Correct |
289 ms |
48228 KB |
Output is correct |
26 |
Correct |
295 ms |
46748 KB |
Output is correct |
27 |
Correct |
4 ms |
7764 KB |
Output is correct |
28 |
Correct |
5 ms |
7764 KB |
Output is correct |
29 |
Correct |
5 ms |
7764 KB |
Output is correct |
30 |
Correct |
6 ms |
7752 KB |
Output is correct |
31 |
Correct |
5 ms |
7752 KB |
Output is correct |
32 |
Correct |
5 ms |
7756 KB |
Output is correct |
33 |
Correct |
4 ms |
7832 KB |
Output is correct |
34 |
Correct |
4 ms |
7764 KB |
Output is correct |
35 |
Correct |
4 ms |
7760 KB |
Output is correct |
36 |
Correct |
15 ms |
11668 KB |
Output is correct |
37 |
Correct |
126 ms |
37896 KB |
Output is correct |
38 |
Correct |
128 ms |
37820 KB |
Output is correct |
39 |
Correct |
131 ms |
37812 KB |
Output is correct |
40 |
Correct |
141 ms |
37416 KB |
Output is correct |
41 |
Correct |
125 ms |
37412 KB |
Output is correct |
42 |
Correct |
116 ms |
34752 KB |
Output is correct |
43 |
Correct |
129 ms |
38068 KB |
Output is correct |
44 |
Correct |
125 ms |
37856 KB |
Output is correct |
45 |
Correct |
95 ms |
41920 KB |
Output is correct |
46 |
Correct |
127 ms |
38164 KB |
Output is correct |
47 |
Correct |
21 ms |
11796 KB |
Output is correct |
48 |
Correct |
267 ms |
42796 KB |
Output is correct |
49 |
Correct |
269 ms |
42772 KB |
Output is correct |
50 |
Correct |
258 ms |
42660 KB |
Output is correct |
51 |
Correct |
264 ms |
42492 KB |
Output is correct |
52 |
Correct |
251 ms |
40848 KB |
Output is correct |
53 |
Correct |
207 ms |
33612 KB |
Output is correct |
54 |
Correct |
272 ms |
43584 KB |
Output is correct |
55 |
Correct |
269 ms |
42660 KB |
Output is correct |
56 |
Correct |
318 ms |
46116 KB |
Output is correct |
57 |
Correct |
269 ms |
43684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
3 ms |
7508 KB |
Output is correct |
3 |
Correct |
4 ms |
7508 KB |
Output is correct |
4 |
Correct |
3 ms |
7508 KB |
Output is correct |
5 |
Correct |
4 ms |
7636 KB |
Output is correct |
6 |
Correct |
7 ms |
7764 KB |
Output is correct |
7 |
Correct |
4 ms |
7752 KB |
Output is correct |
8 |
Correct |
5 ms |
7824 KB |
Output is correct |
9 |
Correct |
6 ms |
7764 KB |
Output is correct |
10 |
Correct |
104 ms |
29888 KB |
Output is correct |
11 |
Correct |
120 ms |
34920 KB |
Output is correct |
12 |
Correct |
119 ms |
34488 KB |
Output is correct |
13 |
Correct |
132 ms |
36068 KB |
Output is correct |
14 |
Correct |
92 ms |
39192 KB |
Output is correct |
15 |
Correct |
121 ms |
29964 KB |
Output is correct |
16 |
Correct |
257 ms |
36940 KB |
Output is correct |
17 |
Correct |
255 ms |
36152 KB |
Output is correct |
18 |
Correct |
269 ms |
37760 KB |
Output is correct |
19 |
Correct |
327 ms |
40888 KB |
Output is correct |
20 |
Correct |
281 ms |
43448 KB |
Output is correct |
21 |
Correct |
286 ms |
48924 KB |
Output is correct |
22 |
Correct |
291 ms |
48088 KB |
Output is correct |
23 |
Correct |
293 ms |
48932 KB |
Output is correct |
24 |
Correct |
293 ms |
48576 KB |
Output is correct |
25 |
Correct |
287 ms |
47032 KB |
Output is correct |
26 |
Correct |
289 ms |
48228 KB |
Output is correct |
27 |
Correct |
295 ms |
46748 KB |
Output is correct |
28 |
Correct |
4 ms |
7764 KB |
Output is correct |
29 |
Correct |
5 ms |
7764 KB |
Output is correct |
30 |
Correct |
5 ms |
7764 KB |
Output is correct |
31 |
Correct |
6 ms |
7752 KB |
Output is correct |
32 |
Correct |
5 ms |
7752 KB |
Output is correct |
33 |
Correct |
5 ms |
7756 KB |
Output is correct |
34 |
Correct |
4 ms |
7832 KB |
Output is correct |
35 |
Correct |
4 ms |
7764 KB |
Output is correct |
36 |
Correct |
4 ms |
7760 KB |
Output is correct |
37 |
Correct |
15 ms |
11668 KB |
Output is correct |
38 |
Correct |
126 ms |
37896 KB |
Output is correct |
39 |
Correct |
128 ms |
37820 KB |
Output is correct |
40 |
Correct |
131 ms |
37812 KB |
Output is correct |
41 |
Correct |
141 ms |
37416 KB |
Output is correct |
42 |
Correct |
125 ms |
37412 KB |
Output is correct |
43 |
Correct |
116 ms |
34752 KB |
Output is correct |
44 |
Correct |
129 ms |
38068 KB |
Output is correct |
45 |
Correct |
125 ms |
37856 KB |
Output is correct |
46 |
Correct |
95 ms |
41920 KB |
Output is correct |
47 |
Correct |
127 ms |
38164 KB |
Output is correct |
48 |
Correct |
21 ms |
11796 KB |
Output is correct |
49 |
Correct |
267 ms |
42796 KB |
Output is correct |
50 |
Correct |
269 ms |
42772 KB |
Output is correct |
51 |
Correct |
258 ms |
42660 KB |
Output is correct |
52 |
Correct |
264 ms |
42492 KB |
Output is correct |
53 |
Correct |
251 ms |
40848 KB |
Output is correct |
54 |
Correct |
207 ms |
33612 KB |
Output is correct |
55 |
Correct |
272 ms |
43584 KB |
Output is correct |
56 |
Correct |
269 ms |
42660 KB |
Output is correct |
57 |
Correct |
318 ms |
46116 KB |
Output is correct |
58 |
Correct |
269 ms |
43684 KB |
Output is correct |
59 |
Correct |
86 ms |
15524 KB |
Output is correct |
60 |
Correct |
256 ms |
38192 KB |
Output is correct |
61 |
Correct |
270 ms |
37272 KB |
Output is correct |
62 |
Correct |
287 ms |
39220 KB |
Output is correct |
63 |
Correct |
316 ms |
42392 KB |
Output is correct |
64 |
Correct |
6 ms |
7892 KB |
Output is correct |
65 |
Correct |
5 ms |
7716 KB |
Output is correct |
66 |
Correct |
5 ms |
7752 KB |
Output is correct |
67 |
Correct |
5 ms |
7924 KB |
Output is correct |
68 |
Correct |
4 ms |
7760 KB |
Output is correct |
69 |
Correct |
5 ms |
8020 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 |
7764 KB |
Output is correct |
73 |
Correct |
5 ms |
8020 KB |
Output is correct |
74 |
Correct |
147 ms |
50776 KB |
Output is correct |
75 |
Correct |
135 ms |
37896 KB |
Output is correct |
76 |
Correct |
128 ms |
38024 KB |
Output is correct |
77 |
Correct |
124 ms |
42168 KB |
Output is correct |
78 |
Correct |
99 ms |
47972 KB |
Output is correct |
79 |
Correct |
78 ms |
39104 KB |
Output is correct |
80 |
Correct |
119 ms |
45108 KB |
Output is correct |
81 |
Correct |
177 ms |
54148 KB |
Output is correct |
82 |
Correct |
196 ms |
57464 KB |
Output is correct |
83 |
Correct |
197 ms |
63712 KB |
Output is correct |
84 |
Correct |
93 ms |
41576 KB |
Output is correct |
85 |
Correct |
181 ms |
58716 KB |
Output is correct |
86 |
Correct |
84 ms |
23544 KB |
Output is correct |
87 |
Correct |
252 ms |
42788 KB |
Output is correct |
88 |
Correct |
255 ms |
42780 KB |
Output is correct |
89 |
Correct |
297 ms |
45636 KB |
Output is correct |
90 |
Correct |
172 ms |
54300 KB |
Output is correct |
91 |
Correct |
182 ms |
50984 KB |
Output is correct |
92 |
Correct |
278 ms |
48600 KB |
Output is correct |
93 |
Correct |
306 ms |
58136 KB |
Output is correct |
94 |
Correct |
396 ms |
61720 KB |
Output is correct |
95 |
Correct |
343 ms |
67768 KB |
Output is correct |
96 |
Correct |
314 ms |
46376 KB |
Output is correct |
97 |
Correct |
344 ms |
54100 KB |
Output is correct |