#include "swap.h"
#include <bits/stdc++.h>
#include <vector>
using namespace std;
const int MX=3e5+5;
int sz[MX];
int p[MX][20];
int nn,n;
int pr[MX];
int w[MX];
int dth[MX];
int b[MX];
int E[MX];
int c[MX];
int deg[MX];
int mdeg[MX];
vector<int> a[MX];
struct A{
int u,v,w;
bool operator <(const A&r)const{
return w<r.w;
}
}e[MX];
int lca(int v,int u){
int i,j;
if(dth[v]>dth[u])swap(u,v);
for(i=19 ; i>=0 ; i--)if(dth[u]-dth[v]>=(1<<i))u=p[u][i];
if(u==v)return v;
for(i=19 ; i>=0 ; i--){
if(p[u][i]!=p[v][i]){
u=p[u][i];
v=p[v][i];
}
}
return p[v][0];
}
int f(int k){
return k==pr[k] ? k:pr[k]=f(pr[k]);
}
void Union(A k){
deg[k.u]++;
deg[k.v]++;
int x=f(k.u);
int y=f(k.v);
if(x==y){
E[x]++;
mdeg[x]=max({mdeg[x],deg[k.u],deg[k.v]});
if(b[x]==-1)
b[x]=k.w;
return;
}
pr[x]=nn;
pr[y]=nn;
mdeg[nn]=max({mdeg[x],mdeg[y],deg[k.u],deg[k.v]});
sz[nn]=sz[x]+sz[y];
E[nn]=E[x]+E[y]+1;
w[nn]=k.w;
a[nn].push_back(x);
a[nn].push_back(y);
p[x][0]=nn;
p[y][0]=nn;
if(E[nn]>=sz[nn] && b[nn]==-1 ){
b[nn]=k.w;
}
if(mdeg[nn]>=3 && b[nn]==-1)
b[nn]=k.w;
nn++;
}
void dfs(int v,int pp){
if(E[v]>=sz[v] || mdeg[v]>=3)c[v]=v;
else if(p[v][0]>=0)c[v]=c[p[v][0]];
else c[v]=-1;
for(auto u:a[v]){
if(u==pp)continue;
dth[u]=dth[v]+1;
dfs(u,v);
}
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
nn=n=N;
int i,j;
for(i=0 ; i<M; i++){
e[i]={U[i],V[i],W[i]};
}
sort(e,e+M);
for(i=0 ;i<N ; i++)sz[i]=1;
for(i=0 ; i<N+M ; i++)pr[i]=i,p[i][0]=-1,b[i]=-1;
for(i=0 ; i<M ; i++){
Union(e[i]);
}
for(i=0 ; i<nn ; i++)
if(p[i][0]==-1)
dfs(i,-1);
for(i=1 ; i<20 ; i++){
for(j=0 ; j<nn ; j++){
p[j][i]=p[p[j][i-1]][i-1];
}
}
}
int getMinimumFuelCapacity(int X, int Y) {
int k=lca(X,Y);
if(c[k]==-1)return -1;
return b[c[k]];
}
Compilation message
swap.cpp: In function 'int lca(int, int)':
swap.cpp:27:11: warning: unused variable 'j' [-Wunused-variable]
27 | int i,j;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7660 KB |
Output is correct |
6 |
Correct |
6 ms |
7660 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
125 ms |
29284 KB |
Output is correct |
10 |
Correct |
151 ms |
34148 KB |
Output is correct |
11 |
Correct |
149 ms |
33764 KB |
Output is correct |
12 |
Correct |
159 ms |
35300 KB |
Output is correct |
13 |
Correct |
147 ms |
38500 KB |
Output is correct |
14 |
Correct |
133 ms |
29416 KB |
Output is correct |
15 |
Correct |
306 ms |
36208 KB |
Output is correct |
16 |
Correct |
291 ms |
35344 KB |
Output is correct |
17 |
Correct |
300 ms |
37188 KB |
Output is correct |
18 |
Correct |
418 ms |
40140 KB |
Output is correct |
19 |
Correct |
105 ms |
15332 KB |
Output is correct |
20 |
Correct |
295 ms |
37476 KB |
Output is correct |
21 |
Correct |
289 ms |
36512 KB |
Output is correct |
22 |
Correct |
308 ms |
38472 KB |
Output is correct |
23 |
Correct |
413 ms |
41680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
382 ms |
42892 KB |
Output is correct |
4 |
Correct |
386 ms |
48204 KB |
Output is correct |
5 |
Correct |
390 ms |
47608 KB |
Output is correct |
6 |
Correct |
365 ms |
47948 KB |
Output is correct |
7 |
Correct |
382 ms |
47812 KB |
Output is correct |
8 |
Correct |
367 ms |
46224 KB |
Output is correct |
9 |
Correct |
367 ms |
47580 KB |
Output is correct |
10 |
Correct |
372 ms |
46016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7660 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
6 ms |
7788 KB |
Output is correct |
10 |
Correct |
6 ms |
7660 KB |
Output is correct |
11 |
Correct |
6 ms |
7788 KB |
Output is correct |
12 |
Correct |
6 ms |
7660 KB |
Output is correct |
13 |
Correct |
6 ms |
7788 KB |
Output is correct |
14 |
Correct |
6 ms |
7660 KB |
Output is correct |
15 |
Correct |
6 ms |
7788 KB |
Output is correct |
16 |
Correct |
6 ms |
7788 KB |
Output is correct |
17 |
Correct |
6 ms |
7788 KB |
Output is correct |
18 |
Correct |
6 ms |
7788 KB |
Output is correct |
19 |
Correct |
6 ms |
7660 KB |
Output is correct |
20 |
Correct |
6 ms |
7660 KB |
Output is correct |
21 |
Correct |
6 ms |
7660 KB |
Output is correct |
22 |
Correct |
6 ms |
7660 KB |
Output is correct |
23 |
Correct |
6 ms |
7660 KB |
Output is correct |
24 |
Correct |
7 ms |
7788 KB |
Output is correct |
25 |
Correct |
7 ms |
7916 KB |
Output is correct |
26 |
Correct |
7 ms |
7916 KB |
Output is correct |
27 |
Correct |
6 ms |
7788 KB |
Output is correct |
28 |
Correct |
6 ms |
7916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7660 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
6 ms |
7788 KB |
Output is correct |
10 |
Correct |
125 ms |
29284 KB |
Output is correct |
11 |
Correct |
151 ms |
34148 KB |
Output is correct |
12 |
Correct |
149 ms |
33764 KB |
Output is correct |
13 |
Correct |
159 ms |
35300 KB |
Output is correct |
14 |
Correct |
147 ms |
38500 KB |
Output is correct |
15 |
Correct |
6 ms |
7660 KB |
Output is correct |
16 |
Correct |
6 ms |
7788 KB |
Output is correct |
17 |
Correct |
6 ms |
7660 KB |
Output is correct |
18 |
Correct |
6 ms |
7788 KB |
Output is correct |
19 |
Correct |
6 ms |
7660 KB |
Output is correct |
20 |
Correct |
6 ms |
7788 KB |
Output is correct |
21 |
Correct |
6 ms |
7788 KB |
Output is correct |
22 |
Correct |
6 ms |
7788 KB |
Output is correct |
23 |
Correct |
6 ms |
7788 KB |
Output is correct |
24 |
Correct |
18 ms |
11500 KB |
Output is correct |
25 |
Correct |
155 ms |
37220 KB |
Output is correct |
26 |
Correct |
164 ms |
37092 KB |
Output is correct |
27 |
Correct |
161 ms |
36964 KB |
Output is correct |
28 |
Correct |
152 ms |
36708 KB |
Output is correct |
29 |
Correct |
155 ms |
36448 KB |
Output is correct |
30 |
Correct |
139 ms |
34268 KB |
Output is correct |
31 |
Correct |
163 ms |
37424 KB |
Output is correct |
32 |
Correct |
156 ms |
37092 KB |
Output is correct |
33 |
Correct |
141 ms |
41188 KB |
Output is correct |
34 |
Correct |
162 ms |
37476 KB |
Output is correct |
35 |
Correct |
6 ms |
7660 KB |
Output is correct |
36 |
Correct |
6 ms |
7660 KB |
Output is correct |
37 |
Correct |
6 ms |
7660 KB |
Output is correct |
38 |
Correct |
6 ms |
7660 KB |
Output is correct |
39 |
Correct |
6 ms |
7660 KB |
Output is correct |
40 |
Correct |
7 ms |
7788 KB |
Output is correct |
41 |
Correct |
7 ms |
7916 KB |
Output is correct |
42 |
Correct |
7 ms |
7916 KB |
Output is correct |
43 |
Correct |
6 ms |
7788 KB |
Output is correct |
44 |
Correct |
6 ms |
7916 KB |
Output is correct |
45 |
Correct |
163 ms |
42216 KB |
Output is correct |
46 |
Correct |
155 ms |
37220 KB |
Output is correct |
47 |
Correct |
153 ms |
37092 KB |
Output is correct |
48 |
Correct |
169 ms |
39780 KB |
Output is correct |
49 |
Correct |
81 ms |
29540 KB |
Output is correct |
50 |
Correct |
83 ms |
25696 KB |
Output is correct |
51 |
Correct |
135 ms |
36960 KB |
Output is correct |
52 |
Correct |
192 ms |
46052 KB |
Output is correct |
53 |
Correct |
207 ms |
49380 KB |
Output is correct |
54 |
Correct |
217 ms |
51416 KB |
Output is correct |
55 |
Correct |
140 ms |
40948 KB |
Output is correct |
56 |
Correct |
204 ms |
49656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
5 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7660 KB |
Output is correct |
6 |
Correct |
6 ms |
7660 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
125 ms |
29284 KB |
Output is correct |
10 |
Correct |
151 ms |
34148 KB |
Output is correct |
11 |
Correct |
149 ms |
33764 KB |
Output is correct |
12 |
Correct |
159 ms |
35300 KB |
Output is correct |
13 |
Correct |
147 ms |
38500 KB |
Output is correct |
14 |
Correct |
133 ms |
29416 KB |
Output is correct |
15 |
Correct |
306 ms |
36208 KB |
Output is correct |
16 |
Correct |
291 ms |
35344 KB |
Output is correct |
17 |
Correct |
300 ms |
37188 KB |
Output is correct |
18 |
Correct |
418 ms |
40140 KB |
Output is correct |
19 |
Correct |
382 ms |
42892 KB |
Output is correct |
20 |
Correct |
386 ms |
48204 KB |
Output is correct |
21 |
Correct |
390 ms |
47608 KB |
Output is correct |
22 |
Correct |
365 ms |
47948 KB |
Output is correct |
23 |
Correct |
382 ms |
47812 KB |
Output is correct |
24 |
Correct |
367 ms |
46224 KB |
Output is correct |
25 |
Correct |
367 ms |
47580 KB |
Output is correct |
26 |
Correct |
372 ms |
46016 KB |
Output is correct |
27 |
Correct |
6 ms |
7660 KB |
Output is correct |
28 |
Correct |
6 ms |
7788 KB |
Output is correct |
29 |
Correct |
6 ms |
7660 KB |
Output is correct |
30 |
Correct |
6 ms |
7788 KB |
Output is correct |
31 |
Correct |
6 ms |
7660 KB |
Output is correct |
32 |
Correct |
6 ms |
7788 KB |
Output is correct |
33 |
Correct |
6 ms |
7788 KB |
Output is correct |
34 |
Correct |
6 ms |
7788 KB |
Output is correct |
35 |
Correct |
6 ms |
7788 KB |
Output is correct |
36 |
Correct |
18 ms |
11500 KB |
Output is correct |
37 |
Correct |
155 ms |
37220 KB |
Output is correct |
38 |
Correct |
164 ms |
37092 KB |
Output is correct |
39 |
Correct |
161 ms |
36964 KB |
Output is correct |
40 |
Correct |
152 ms |
36708 KB |
Output is correct |
41 |
Correct |
155 ms |
36448 KB |
Output is correct |
42 |
Correct |
139 ms |
34268 KB |
Output is correct |
43 |
Correct |
163 ms |
37424 KB |
Output is correct |
44 |
Correct |
156 ms |
37092 KB |
Output is correct |
45 |
Correct |
141 ms |
41188 KB |
Output is correct |
46 |
Correct |
162 ms |
37476 KB |
Output is correct |
47 |
Correct |
28 ms |
11628 KB |
Output is correct |
48 |
Correct |
317 ms |
41908 KB |
Output is correct |
49 |
Correct |
327 ms |
42244 KB |
Output is correct |
50 |
Correct |
308 ms |
41800 KB |
Output is correct |
51 |
Correct |
301 ms |
41816 KB |
Output is correct |
52 |
Correct |
309 ms |
40080 KB |
Output is correct |
53 |
Correct |
251 ms |
33244 KB |
Output is correct |
54 |
Correct |
310 ms |
42824 KB |
Output is correct |
55 |
Correct |
315 ms |
42008 KB |
Output is correct |
56 |
Correct |
425 ms |
45256 KB |
Output is correct |
57 |
Correct |
337 ms |
42952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7660 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
6 ms |
7788 KB |
Output is correct |
10 |
Correct |
125 ms |
29284 KB |
Output is correct |
11 |
Correct |
151 ms |
34148 KB |
Output is correct |
12 |
Correct |
149 ms |
33764 KB |
Output is correct |
13 |
Correct |
159 ms |
35300 KB |
Output is correct |
14 |
Correct |
147 ms |
38500 KB |
Output is correct |
15 |
Correct |
133 ms |
29416 KB |
Output is correct |
16 |
Correct |
306 ms |
36208 KB |
Output is correct |
17 |
Correct |
291 ms |
35344 KB |
Output is correct |
18 |
Correct |
300 ms |
37188 KB |
Output is correct |
19 |
Correct |
418 ms |
40140 KB |
Output is correct |
20 |
Correct |
382 ms |
42892 KB |
Output is correct |
21 |
Correct |
386 ms |
48204 KB |
Output is correct |
22 |
Correct |
390 ms |
47608 KB |
Output is correct |
23 |
Correct |
365 ms |
47948 KB |
Output is correct |
24 |
Correct |
382 ms |
47812 KB |
Output is correct |
25 |
Correct |
367 ms |
46224 KB |
Output is correct |
26 |
Correct |
367 ms |
47580 KB |
Output is correct |
27 |
Correct |
372 ms |
46016 KB |
Output is correct |
28 |
Correct |
6 ms |
7660 KB |
Output is correct |
29 |
Correct |
6 ms |
7788 KB |
Output is correct |
30 |
Correct |
6 ms |
7660 KB |
Output is correct |
31 |
Correct |
6 ms |
7788 KB |
Output is correct |
32 |
Correct |
6 ms |
7660 KB |
Output is correct |
33 |
Correct |
6 ms |
7788 KB |
Output is correct |
34 |
Correct |
6 ms |
7788 KB |
Output is correct |
35 |
Correct |
6 ms |
7788 KB |
Output is correct |
36 |
Correct |
6 ms |
7788 KB |
Output is correct |
37 |
Correct |
18 ms |
11500 KB |
Output is correct |
38 |
Correct |
155 ms |
37220 KB |
Output is correct |
39 |
Correct |
164 ms |
37092 KB |
Output is correct |
40 |
Correct |
161 ms |
36964 KB |
Output is correct |
41 |
Correct |
152 ms |
36708 KB |
Output is correct |
42 |
Correct |
155 ms |
36448 KB |
Output is correct |
43 |
Correct |
139 ms |
34268 KB |
Output is correct |
44 |
Correct |
163 ms |
37424 KB |
Output is correct |
45 |
Correct |
156 ms |
37092 KB |
Output is correct |
46 |
Correct |
141 ms |
41188 KB |
Output is correct |
47 |
Correct |
162 ms |
37476 KB |
Output is correct |
48 |
Correct |
28 ms |
11628 KB |
Output is correct |
49 |
Correct |
317 ms |
41908 KB |
Output is correct |
50 |
Correct |
327 ms |
42244 KB |
Output is correct |
51 |
Correct |
308 ms |
41800 KB |
Output is correct |
52 |
Correct |
301 ms |
41816 KB |
Output is correct |
53 |
Correct |
309 ms |
40080 KB |
Output is correct |
54 |
Correct |
251 ms |
33244 KB |
Output is correct |
55 |
Correct |
310 ms |
42824 KB |
Output is correct |
56 |
Correct |
315 ms |
42008 KB |
Output is correct |
57 |
Correct |
425 ms |
45256 KB |
Output is correct |
58 |
Correct |
337 ms |
42952 KB |
Output is correct |
59 |
Correct |
105 ms |
15332 KB |
Output is correct |
60 |
Correct |
295 ms |
37476 KB |
Output is correct |
61 |
Correct |
289 ms |
36512 KB |
Output is correct |
62 |
Correct |
308 ms |
38472 KB |
Output is correct |
63 |
Correct |
413 ms |
41680 KB |
Output is correct |
64 |
Correct |
6 ms |
7660 KB |
Output is correct |
65 |
Correct |
6 ms |
7660 KB |
Output is correct |
66 |
Correct |
6 ms |
7660 KB |
Output is correct |
67 |
Correct |
6 ms |
7660 KB |
Output is correct |
68 |
Correct |
6 ms |
7660 KB |
Output is correct |
69 |
Correct |
7 ms |
7788 KB |
Output is correct |
70 |
Correct |
7 ms |
7916 KB |
Output is correct |
71 |
Correct |
7 ms |
7916 KB |
Output is correct |
72 |
Correct |
6 ms |
7788 KB |
Output is correct |
73 |
Correct |
6 ms |
7916 KB |
Output is correct |
74 |
Correct |
163 ms |
42216 KB |
Output is correct |
75 |
Correct |
155 ms |
37220 KB |
Output is correct |
76 |
Correct |
153 ms |
37092 KB |
Output is correct |
77 |
Correct |
169 ms |
39780 KB |
Output is correct |
78 |
Correct |
81 ms |
29540 KB |
Output is correct |
79 |
Correct |
83 ms |
25696 KB |
Output is correct |
80 |
Correct |
135 ms |
36960 KB |
Output is correct |
81 |
Correct |
192 ms |
46052 KB |
Output is correct |
82 |
Correct |
207 ms |
49380 KB |
Output is correct |
83 |
Correct |
217 ms |
51416 KB |
Output is correct |
84 |
Correct |
140 ms |
40948 KB |
Output is correct |
85 |
Correct |
204 ms |
49656 KB |
Output is correct |
86 |
Correct |
105 ms |
21288 KB |
Output is correct |
87 |
Correct |
307 ms |
42060 KB |
Output is correct |
88 |
Correct |
302 ms |
41928 KB |
Output is correct |
89 |
Correct |
352 ms |
43012 KB |
Output is correct |
90 |
Correct |
186 ms |
34880 KB |
Output is correct |
91 |
Correct |
208 ms |
34824 KB |
Output is correct |
92 |
Correct |
298 ms |
41280 KB |
Output is correct |
93 |
Correct |
339 ms |
50716 KB |
Output is correct |
94 |
Correct |
381 ms |
53444 KB |
Output is correct |
95 |
Correct |
361 ms |
55364 KB |
Output is correct |
96 |
Correct |
422 ms |
45640 KB |
Output is correct |
97 |
Correct |
386 ms |
49116 KB |
Output is correct |