#include "swap.h"
#include <iostream>
#include <vector>
using namespace std;
long long int f2[1000005],can=0,fa[1000005][21],deep[1000005],p[1000005],cnt,n,m,deg[1000005],u[1000005],v[1000005],w[1000005],f[1000005],ok[1000005],num[1000005],d[1000005];
int find(int x)
{
if(x==f2[x])return f2[x];
return f2[x]=find(f2[x]);
}
void qs(int s,int e)
{
if(s==e)return ;
long long int l=s,r=e,mid=w[(s+e)/2];
while(l<=r)
{
while(w[l]<mid)l++;
while(w[r]>mid)r--;
if(l<=r)
{
swap(w[l],w[r]);
swap(u[l],u[r]);
swap(v[l],v[r]);
l++;
r--;
}
}
if(r>s)qs(s,r);
if(e>l)qs(l,e);
}
void update(int x)
{
if(p[x]!=-1)return ;
//cout<<x<<" "<<ok[x]<<" "<<f[x]<<endl;
if(ok[x]==1)
{
p[x]=x;
return ;
}
update(f[x]);
p[x]=p[f[x]];
return ;
}
void update2(int x)
{
if(deep[x]!=0)return ;
if(f[x]==x)
{
deep[x]=1;
return ;
}
update2(f[x]);
deep[x]=deep[f[x]]+1;
return ;
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n=N;
m=M;
cnt=n-1;
for(int i=0;i<m;i++)
{
u[i]=U[i];
v[i]=V[i];
w[i]=W[i];
}
qs(0,m-1);
long long int ma=0;
for(int i=0;i<=1000000;i++)
{
f2[i]=i;
f[i]=i;
}
for(int i=0;i<m;i++)
{
long long int x=u[i],y=v[i];
deg[x]++;
deg[y]++;
long long int fx=find(x),fy=find(y);
//cout<<fx<<" "<<fy<<endl;
if(fx==fy)
{
if(ok[fx]==0)
{
can=1;
ok[fx]=1;
num[fx]=w[i];
}
}
else
{
if(ok[fx]==1 || ok[fy]==1 || deg[x]>=3 || deg[y]>=3)
{
cnt++;
//cout<<cnt<<" "<<x<<" "<<y<<endl;
can=1;
f[fx]=cnt;
f[fy]=cnt;
f2[fx]=cnt;
f2[fy]=cnt;
ok[cnt]=1;
num[cnt]=w[i];
}
else
{
f[fy]=fx;
f2[fy]=fx;
}
}
}
if(can==0)return ;
for(int i=0;i<=cnt;i++)
{
p[i]=-1;
}
for(int i=0;i<=cnt;i++)
{
if(p[i]==-1)update(i);
//cout<<i<<" "<<p[i]<<endl;
//cout<<i<<" "<<f[i]<<endl;
}
for(int i=0;i<=cnt;i++)
{
if(deep[i]==0)update2(i);
}
for(int i=0;i<=cnt;i++)
{
fa[i][0]=f[p[i]];
if(f[p[i]]==p[i])fa[i][0]=-1;
}
for(int i=1;i<=20;i++)
{
for(int j=0;j<=cnt;j++)
{
if(fa[j][i-1]==-1)continue;
fa[j][i]=fa[fa[j][i-1]][i-1];
}
}
return ;
}
int lca(int x,int y)
{
if(deep[x]<deep[y])swap(x,y);
//cout<<x<<" "<<y<<" "<<deep[x]<<" "<<deep[y]<<endl;
int tmp=deep[x]-deep[y];
int pp=0;
while(tmp>0)
{
if(tmp%2==1)x=fa[x][pp];
tmp/=2;
pp++;
}
if(x==y)return num[x];
for(int i=20;i>=0;i--)
{
if(fa[x][i]!=fa[y][i] && fa[x][i]!=-1)
{
x=fa[x][i];
y=fa[y][i];
}
}
return num[f[x]];
}
int getMinimumFuelCapacity(int X, int Y) {
if(can==0)return -1;
int x=p[X],y=p[Y];
//cout<<" "<<x<<" "<<y<<endl;
int ans=lca(x,y);
if(ans==0)return -1;
return ans;
}
Compilation message
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:67:16: warning: unused variable 'ma' [-Wunused-variable]
67 | long long int ma=0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
10 ms |
16000 KB |
Output is correct |
5 |
Correct |
12 ms |
16000 KB |
Output is correct |
6 |
Correct |
10 ms |
16000 KB |
Output is correct |
7 |
Correct |
10 ms |
16000 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
56 ms |
20344 KB |
Output is correct |
10 |
Correct |
67 ms |
21240 KB |
Output is correct |
11 |
Correct |
66 ms |
21240 KB |
Output is correct |
12 |
Correct |
70 ms |
21496 KB |
Output is correct |
13 |
Correct |
69 ms |
22008 KB |
Output is correct |
14 |
Correct |
64 ms |
20472 KB |
Output is correct |
15 |
Correct |
140 ms |
23172 KB |
Output is correct |
16 |
Correct |
143 ms |
23208 KB |
Output is correct |
17 |
Correct |
147 ms |
23264 KB |
Output is correct |
18 |
Correct |
142 ms |
23648 KB |
Output is correct |
19 |
Correct |
83 ms |
23236 KB |
Output is correct |
20 |
Correct |
175 ms |
41848 KB |
Output is correct |
21 |
Correct |
176 ms |
41140 KB |
Output is correct |
22 |
Correct |
188 ms |
42720 KB |
Output is correct |
23 |
Correct |
186 ms |
43104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
333 ms |
61336 KB |
Output is correct |
4 |
Correct |
333 ms |
63612 KB |
Output is correct |
5 |
Correct |
335 ms |
62220 KB |
Output is correct |
6 |
Correct |
341 ms |
63200 KB |
Output is correct |
7 |
Correct |
342 ms |
62836 KB |
Output is correct |
8 |
Correct |
333 ms |
61092 KB |
Output is correct |
9 |
Correct |
334 ms |
62580 KB |
Output is correct |
10 |
Correct |
336 ms |
60720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
10 ms |
16000 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
12 ms |
16000 KB |
Output is correct |
7 |
Correct |
10 ms |
16000 KB |
Output is correct |
8 |
Correct |
10 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
11 ms |
16384 KB |
Output is correct |
11 |
Correct |
10 ms |
16256 KB |
Output is correct |
12 |
Correct |
10 ms |
16256 KB |
Output is correct |
13 |
Correct |
11 ms |
16256 KB |
Output is correct |
14 |
Correct |
10 ms |
16256 KB |
Output is correct |
15 |
Correct |
10 ms |
16256 KB |
Output is correct |
16 |
Correct |
11 ms |
16256 KB |
Output is correct |
17 |
Correct |
10 ms |
16384 KB |
Output is correct |
18 |
Correct |
10 ms |
16384 KB |
Output is correct |
19 |
Correct |
10 ms |
16256 KB |
Output is correct |
20 |
Correct |
10 ms |
16256 KB |
Output is correct |
21 |
Correct |
10 ms |
16384 KB |
Output is correct |
22 |
Correct |
11 ms |
16128 KB |
Output is correct |
23 |
Correct |
10 ms |
16384 KB |
Output is correct |
24 |
Correct |
11 ms |
16384 KB |
Output is correct |
25 |
Correct |
11 ms |
16384 KB |
Output is correct |
26 |
Correct |
11 ms |
16384 KB |
Output is correct |
27 |
Correct |
11 ms |
16384 KB |
Output is correct |
28 |
Correct |
11 ms |
16384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
10 ms |
16000 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
12 ms |
16000 KB |
Output is correct |
7 |
Correct |
10 ms |
16000 KB |
Output is correct |
8 |
Correct |
10 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
56 ms |
20344 KB |
Output is correct |
11 |
Correct |
67 ms |
21240 KB |
Output is correct |
12 |
Correct |
66 ms |
21240 KB |
Output is correct |
13 |
Correct |
70 ms |
21496 KB |
Output is correct |
14 |
Correct |
69 ms |
22008 KB |
Output is correct |
15 |
Correct |
11 ms |
16384 KB |
Output is correct |
16 |
Correct |
10 ms |
16256 KB |
Output is correct |
17 |
Correct |
10 ms |
16256 KB |
Output is correct |
18 |
Correct |
11 ms |
16256 KB |
Output is correct |
19 |
Correct |
10 ms |
16256 KB |
Output is correct |
20 |
Correct |
10 ms |
16256 KB |
Output is correct |
21 |
Correct |
11 ms |
16256 KB |
Output is correct |
22 |
Correct |
10 ms |
16384 KB |
Output is correct |
23 |
Correct |
10 ms |
16384 KB |
Output is correct |
24 |
Correct |
21 ms |
20480 KB |
Output is correct |
25 |
Correct |
102 ms |
39544 KB |
Output is correct |
26 |
Correct |
101 ms |
39544 KB |
Output is correct |
27 |
Correct |
100 ms |
39796 KB |
Output is correct |
28 |
Correct |
101 ms |
40184 KB |
Output is correct |
29 |
Correct |
107 ms |
42872 KB |
Output is correct |
30 |
Correct |
109 ms |
43768 KB |
Output is correct |
31 |
Correct |
103 ms |
39544 KB |
Output is correct |
32 |
Correct |
99 ms |
39520 KB |
Output is correct |
33 |
Correct |
151 ms |
58876 KB |
Output is correct |
34 |
Correct |
129 ms |
48632 KB |
Output is correct |
35 |
Correct |
10 ms |
16256 KB |
Output is correct |
36 |
Correct |
10 ms |
16256 KB |
Output is correct |
37 |
Correct |
10 ms |
16384 KB |
Output is correct |
38 |
Correct |
11 ms |
16128 KB |
Output is correct |
39 |
Correct |
10 ms |
16384 KB |
Output is correct |
40 |
Correct |
11 ms |
16384 KB |
Output is correct |
41 |
Correct |
11 ms |
16384 KB |
Output is correct |
42 |
Correct |
11 ms |
16384 KB |
Output is correct |
43 |
Correct |
11 ms |
16384 KB |
Output is correct |
44 |
Correct |
11 ms |
16384 KB |
Output is correct |
45 |
Correct |
155 ms |
46328 KB |
Output is correct |
46 |
Correct |
99 ms |
39544 KB |
Output is correct |
47 |
Correct |
104 ms |
40824 KB |
Output is correct |
48 |
Correct |
137 ms |
49144 KB |
Output is correct |
49 |
Correct |
94 ms |
23800 KB |
Output is correct |
50 |
Correct |
78 ms |
23928 KB |
Output is correct |
51 |
Correct |
131 ms |
41080 KB |
Output is correct |
52 |
Correct |
144 ms |
43000 KB |
Output is correct |
53 |
Correct |
174 ms |
51612 KB |
Output is correct |
54 |
Correct |
169 ms |
44280 KB |
Output is correct |
55 |
Correct |
154 ms |
59256 KB |
Output is correct |
56 |
Correct |
190 ms |
54036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
10 ms |
16000 KB |
Output is correct |
5 |
Correct |
12 ms |
16000 KB |
Output is correct |
6 |
Correct |
10 ms |
16000 KB |
Output is correct |
7 |
Correct |
10 ms |
16000 KB |
Output is correct |
8 |
Correct |
11 ms |
16000 KB |
Output is correct |
9 |
Correct |
56 ms |
20344 KB |
Output is correct |
10 |
Correct |
67 ms |
21240 KB |
Output is correct |
11 |
Correct |
66 ms |
21240 KB |
Output is correct |
12 |
Correct |
70 ms |
21496 KB |
Output is correct |
13 |
Correct |
69 ms |
22008 KB |
Output is correct |
14 |
Correct |
64 ms |
20472 KB |
Output is correct |
15 |
Correct |
140 ms |
23172 KB |
Output is correct |
16 |
Correct |
143 ms |
23208 KB |
Output is correct |
17 |
Correct |
147 ms |
23264 KB |
Output is correct |
18 |
Correct |
142 ms |
23648 KB |
Output is correct |
19 |
Correct |
333 ms |
61336 KB |
Output is correct |
20 |
Correct |
333 ms |
63612 KB |
Output is correct |
21 |
Correct |
335 ms |
62220 KB |
Output is correct |
22 |
Correct |
341 ms |
63200 KB |
Output is correct |
23 |
Correct |
342 ms |
62836 KB |
Output is correct |
24 |
Correct |
333 ms |
61092 KB |
Output is correct |
25 |
Correct |
334 ms |
62580 KB |
Output is correct |
26 |
Correct |
336 ms |
60720 KB |
Output is correct |
27 |
Correct |
11 ms |
16384 KB |
Output is correct |
28 |
Correct |
10 ms |
16256 KB |
Output is correct |
29 |
Correct |
10 ms |
16256 KB |
Output is correct |
30 |
Correct |
11 ms |
16256 KB |
Output is correct |
31 |
Correct |
10 ms |
16256 KB |
Output is correct |
32 |
Correct |
10 ms |
16256 KB |
Output is correct |
33 |
Correct |
11 ms |
16256 KB |
Output is correct |
34 |
Correct |
10 ms |
16384 KB |
Output is correct |
35 |
Correct |
10 ms |
16384 KB |
Output is correct |
36 |
Correct |
21 ms |
20480 KB |
Output is correct |
37 |
Correct |
102 ms |
39544 KB |
Output is correct |
38 |
Correct |
101 ms |
39544 KB |
Output is correct |
39 |
Correct |
100 ms |
39796 KB |
Output is correct |
40 |
Correct |
101 ms |
40184 KB |
Output is correct |
41 |
Correct |
107 ms |
42872 KB |
Output is correct |
42 |
Correct |
109 ms |
43768 KB |
Output is correct |
43 |
Correct |
103 ms |
39544 KB |
Output is correct |
44 |
Correct |
99 ms |
39520 KB |
Output is correct |
45 |
Correct |
151 ms |
58876 KB |
Output is correct |
46 |
Correct |
129 ms |
48632 KB |
Output is correct |
47 |
Correct |
31 ms |
20336 KB |
Output is correct |
48 |
Correct |
193 ms |
42204 KB |
Output is correct |
49 |
Correct |
196 ms |
42208 KB |
Output is correct |
50 |
Correct |
218 ms |
42460 KB |
Output is correct |
51 |
Correct |
209 ms |
42984 KB |
Output is correct |
52 |
Correct |
258 ms |
43816 KB |
Output is correct |
53 |
Correct |
280 ms |
41112 KB |
Output is correct |
54 |
Correct |
197 ms |
42880 KB |
Output is correct |
55 |
Correct |
179 ms |
42096 KB |
Output is correct |
56 |
Correct |
440 ms |
61640 KB |
Output is correct |
57 |
Correct |
316 ms |
51864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16000 KB |
Output is correct |
2 |
Correct |
10 ms |
16000 KB |
Output is correct |
3 |
Correct |
10 ms |
16000 KB |
Output is correct |
4 |
Correct |
10 ms |
16000 KB |
Output is correct |
5 |
Correct |
10 ms |
16000 KB |
Output is correct |
6 |
Correct |
12 ms |
16000 KB |
Output is correct |
7 |
Correct |
10 ms |
16000 KB |
Output is correct |
8 |
Correct |
10 ms |
16000 KB |
Output is correct |
9 |
Correct |
11 ms |
16000 KB |
Output is correct |
10 |
Correct |
56 ms |
20344 KB |
Output is correct |
11 |
Correct |
67 ms |
21240 KB |
Output is correct |
12 |
Correct |
66 ms |
21240 KB |
Output is correct |
13 |
Correct |
70 ms |
21496 KB |
Output is correct |
14 |
Correct |
69 ms |
22008 KB |
Output is correct |
15 |
Correct |
64 ms |
20472 KB |
Output is correct |
16 |
Correct |
140 ms |
23172 KB |
Output is correct |
17 |
Correct |
143 ms |
23208 KB |
Output is correct |
18 |
Correct |
147 ms |
23264 KB |
Output is correct |
19 |
Correct |
142 ms |
23648 KB |
Output is correct |
20 |
Correct |
333 ms |
61336 KB |
Output is correct |
21 |
Correct |
333 ms |
63612 KB |
Output is correct |
22 |
Correct |
335 ms |
62220 KB |
Output is correct |
23 |
Correct |
341 ms |
63200 KB |
Output is correct |
24 |
Correct |
342 ms |
62836 KB |
Output is correct |
25 |
Correct |
333 ms |
61092 KB |
Output is correct |
26 |
Correct |
334 ms |
62580 KB |
Output is correct |
27 |
Correct |
336 ms |
60720 KB |
Output is correct |
28 |
Correct |
11 ms |
16384 KB |
Output is correct |
29 |
Correct |
10 ms |
16256 KB |
Output is correct |
30 |
Correct |
10 ms |
16256 KB |
Output is correct |
31 |
Correct |
11 ms |
16256 KB |
Output is correct |
32 |
Correct |
10 ms |
16256 KB |
Output is correct |
33 |
Correct |
10 ms |
16256 KB |
Output is correct |
34 |
Correct |
11 ms |
16256 KB |
Output is correct |
35 |
Correct |
10 ms |
16384 KB |
Output is correct |
36 |
Correct |
10 ms |
16384 KB |
Output is correct |
37 |
Correct |
21 ms |
20480 KB |
Output is correct |
38 |
Correct |
102 ms |
39544 KB |
Output is correct |
39 |
Correct |
101 ms |
39544 KB |
Output is correct |
40 |
Correct |
100 ms |
39796 KB |
Output is correct |
41 |
Correct |
101 ms |
40184 KB |
Output is correct |
42 |
Correct |
107 ms |
42872 KB |
Output is correct |
43 |
Correct |
109 ms |
43768 KB |
Output is correct |
44 |
Correct |
103 ms |
39544 KB |
Output is correct |
45 |
Correct |
99 ms |
39520 KB |
Output is correct |
46 |
Correct |
151 ms |
58876 KB |
Output is correct |
47 |
Correct |
129 ms |
48632 KB |
Output is correct |
48 |
Correct |
31 ms |
20336 KB |
Output is correct |
49 |
Correct |
193 ms |
42204 KB |
Output is correct |
50 |
Correct |
196 ms |
42208 KB |
Output is correct |
51 |
Correct |
218 ms |
42460 KB |
Output is correct |
52 |
Correct |
209 ms |
42984 KB |
Output is correct |
53 |
Correct |
258 ms |
43816 KB |
Output is correct |
54 |
Correct |
280 ms |
41112 KB |
Output is correct |
55 |
Correct |
197 ms |
42880 KB |
Output is correct |
56 |
Correct |
179 ms |
42096 KB |
Output is correct |
57 |
Correct |
440 ms |
61640 KB |
Output is correct |
58 |
Correct |
316 ms |
51864 KB |
Output is correct |
59 |
Correct |
83 ms |
23236 KB |
Output is correct |
60 |
Correct |
175 ms |
41848 KB |
Output is correct |
61 |
Correct |
176 ms |
41140 KB |
Output is correct |
62 |
Correct |
188 ms |
42720 KB |
Output is correct |
63 |
Correct |
186 ms |
43104 KB |
Output is correct |
64 |
Correct |
10 ms |
16256 KB |
Output is correct |
65 |
Correct |
10 ms |
16256 KB |
Output is correct |
66 |
Correct |
10 ms |
16384 KB |
Output is correct |
67 |
Correct |
11 ms |
16128 KB |
Output is correct |
68 |
Correct |
10 ms |
16384 KB |
Output is correct |
69 |
Correct |
11 ms |
16384 KB |
Output is correct |
70 |
Correct |
11 ms |
16384 KB |
Output is correct |
71 |
Correct |
11 ms |
16384 KB |
Output is correct |
72 |
Correct |
11 ms |
16384 KB |
Output is correct |
73 |
Correct |
11 ms |
16384 KB |
Output is correct |
74 |
Correct |
155 ms |
46328 KB |
Output is correct |
75 |
Correct |
99 ms |
39544 KB |
Output is correct |
76 |
Correct |
104 ms |
40824 KB |
Output is correct |
77 |
Correct |
137 ms |
49144 KB |
Output is correct |
78 |
Correct |
94 ms |
23800 KB |
Output is correct |
79 |
Correct |
78 ms |
23928 KB |
Output is correct |
80 |
Correct |
131 ms |
41080 KB |
Output is correct |
81 |
Correct |
144 ms |
43000 KB |
Output is correct |
82 |
Correct |
174 ms |
51612 KB |
Output is correct |
83 |
Correct |
169 ms |
44280 KB |
Output is correct |
84 |
Correct |
154 ms |
59256 KB |
Output is correct |
85 |
Correct |
190 ms |
54036 KB |
Output is correct |
86 |
Correct |
86 ms |
28004 KB |
Output is correct |
87 |
Correct |
208 ms |
42332 KB |
Output is correct |
88 |
Correct |
211 ms |
42204 KB |
Output is correct |
89 |
Correct |
307 ms |
50584 KB |
Output is correct |
90 |
Correct |
187 ms |
26576 KB |
Output is correct |
91 |
Correct |
204 ms |
28832 KB |
Output is correct |
92 |
Correct |
279 ms |
43736 KB |
Output is correct |
93 |
Correct |
260 ms |
46000 KB |
Output is correct |
94 |
Correct |
373 ms |
52056 KB |
Output is correct |
95 |
Correct |
264 ms |
45868 KB |
Output is correct |
96 |
Correct |
440 ms |
61600 KB |
Output is correct |
97 |
Correct |
371 ms |
54432 KB |
Output is correct |