#include<bits/stdc++.h>
using namespace std;
#include "swap.h"
#define X first
#define Y second
#define pb push_back
int n,m,dg[100005],u_i,v_i,w_i,x,y,type=-1,flag=-1;
int deter=2,d[1005][1005],st,md,ed,u1,u2,w,node,u;
int from[100005],p[100005],id[100005],chk,blk[100005];
int vis[100005];
vector<pair<int,int> > g[100005];
vector<pair<int,pair<int,int> > > v;
queue<pair<int,int> > q;
queue<int> qq;
int f(int a)
{
if(p[a]==a)return a;
return p[a]=f(p[a]);
}
void un(int b,int c)
{
p[f(b)]=f(c);
}
void init(int N, int M,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
n=N;
m=M;
for(int i=0;i<m;i++)
{
u_i=U[i]+1;
v_i=V[i]+1;
w_i=W[i];
g[u_i].pb({v_i,w_i});
g[v_i].pb({u_i,w_i});
++dg[u_i];
++dg[v_i];
v.push_back({w_i,{u_i,v_i}});
if(u_i!=1)flag=0;
}
sort(v.begin(),v.end());
for(int i=1;i<=n;i++)
{
if(dg[i]>2)
{
type=0;
}
}
if(type==-1)
{
deter=0;
for(int i=1;i<=n;i++)if(dg[i]!=2)deter=-1;
}
}
int getMinimumFuelCapacity(int X, int Y)
{
x=X+1;
y=Y+1;
//subtask1
if(type==-1)
{
if(deter==0)return v[v.size()-1].X;
return -1;
}
//subtask2
if(m==n-1&&flag==-1)
{
if(m<=2)return -1;
if(x==1)
{
if(g[y][0].Y>v[2].X)return g[y][0].Y;
return v[2].X;
}
if(x==v[0].Y.Y&&y==v[1].Y.Y||x==v[1].Y.Y&&y==v[0].Y.Y)return v[2].X;
return max(g[x][0].Y,g[y][0].Y);
}
//subtask3
if(n<=1000&&m<=2000)
{
st=0;
ed=v.size()-1;
while(ed>=st)
{
md=(st+ed)/2;
memset(d,0,sizeof d);
d[x][y]=1;
q.push({x,y});
while(!q.empty())
{
u1=q.front().X;
u2=q.front().Y;
q.pop();
for(int i=0;i<g[u1].size();i++)
{
node=g[u1][i].X;
w=g[u1][i].Y;
if(v[md].X>=w&&node!=u2&&d[node][u2]==0)
{
d[node][u2]=1;
q.push({node,u2});
}
}
for(int i=0;i<g[u2].size();i++)
{
node=g[u2][i].X;
w=g[u2][i].Y;
if(v[md].X>=w&&node!=u1&&d[u1][node]==0)
{
d[u1][node]=1;
q.push({u1,node});
}
}
}
if(d[y][x]==1)
{
ed=md-1;
}else
{
st=md+1;
}
}
return v[st].X;
}
//subtask 4
st=0;
ed=v.size()-1;
//printf("initial=%d %d\n",x,y);
while(ed>=st)
{
md=(st+ed)/2;
//printf("group=%d %d %d\n",st,md,ed);
for(int i=1;i<=n;i++)p[i]=i,id[i]=0;
for(int i=0;i<=md;i++)
{
un(v[i].Y.X,v[i].Y.Y);
++id[v[i].Y.X];
++id[v[i].Y.Y];
}
if(f(x)!=f(y))
{
//printf("unequal parent\n");
st=md+1;
}else
{
chk=-1;
for(int i=1;i<=n;i++)if(f(x)==f(i))if(id[i]>=3)chk=0;
if(chk==0)
{
//printf("perfect with branch\n");
ed=md-1;
}else
{
for(int i=1;i<=n;i++)blk[i]=0,from[i]=-1;
from[x]=0;
qq.push(x);
while(!qq.empty())
{
u=qq.front();
qq.pop();
for(int i=0;i<g[u].size();i++)
{
node=g[u][i].X;
w=g[u][i].Y;
if(from[node]==-1&&w<=v[md].X)
{
from[node]=u;
qq.push(node);
}
}
}
u=y;
while(1)
{
u=from[u];
if(u==x)break;
blk[u]=1;
}
for(int i=1;i<=n;i++)vis[i]=0;
vis[x]=1;
qq.push(x);
while(!qq.empty())
{
u=qq.front();
qq.pop();
for(int i=0;i<g[u].size();i++)
{
node=g[u][i].X;
w=g[u][i].Y;
if(u==x&&node==y)continue;
if(blk[node]==0&&vis[node]==0&&w<=v[md].X)
{
vis[node]=1;
qq.push(node);
}
}
}
if(vis[y]==1)
{
//printf("perfect\n");
ed=md-1;
}else
{
//printf("no cycle\n");
st=md+1;
}
}
}
}
return v[st].X;
}
Compilation message
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:82:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
82 | if(x==v[0].Y.Y&&y==v[1].Y.Y||x==v[1].Y.Y&&y==v[0].Y.Y)return v[2].X;
| ^
swap.cpp:102:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<g[u1].size();i++)
| ~^~~~~~~~~~~~~
swap.cpp:112:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i=0;i<g[u2].size();i++)
| ~^~~~~~~~~~~~~
swap.cpp:170:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
170 | for(int i=0;i<g[u].size();i++)
| ~^~~~~~~~~~~~
swap.cpp:195:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
195 | for(int i=0;i<g[u].size();i++)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2680 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
66 ms |
10408 KB |
Output is correct |
10 |
Correct |
77 ms |
11516 KB |
Output is correct |
11 |
Correct |
53 ms |
11404 KB |
Output is correct |
12 |
Correct |
58 ms |
11820 KB |
Output is correct |
13 |
Correct |
58 ms |
11780 KB |
Output is correct |
14 |
Correct |
49 ms |
10660 KB |
Output is correct |
15 |
Correct |
132 ms |
15632 KB |
Output is correct |
16 |
Correct |
109 ms |
15380 KB |
Output is correct |
17 |
Correct |
119 ms |
15820 KB |
Output is correct |
18 |
Correct |
128 ms |
15844 KB |
Output is correct |
19 |
Correct |
57 ms |
9164 KB |
Output is correct |
20 |
Correct |
116 ms |
15792 KB |
Output is correct |
21 |
Correct |
122 ms |
15932 KB |
Output is correct |
22 |
Correct |
122 ms |
16360 KB |
Output is correct |
23 |
Correct |
138 ms |
16380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
108 ms |
16892 KB |
Output is correct |
4 |
Correct |
123 ms |
16420 KB |
Output is correct |
5 |
Correct |
118 ms |
16424 KB |
Output is correct |
6 |
Correct |
104 ms |
16312 KB |
Output is correct |
7 |
Correct |
131 ms |
16588 KB |
Output is correct |
8 |
Correct |
108 ms |
16084 KB |
Output is correct |
9 |
Correct |
106 ms |
16244 KB |
Output is correct |
10 |
Correct |
145 ms |
16092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2680 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
229 ms |
6888 KB |
Output is correct |
11 |
Correct |
292 ms |
6696 KB |
Output is correct |
12 |
Correct |
189 ms |
6712 KB |
Output is correct |
13 |
Correct |
189 ms |
6656 KB |
Output is correct |
14 |
Correct |
364 ms |
6724 KB |
Output is correct |
15 |
Correct |
145 ms |
6612 KB |
Output is correct |
16 |
Correct |
297 ms |
6740 KB |
Output is correct |
17 |
Correct |
261 ms |
6732 KB |
Output is correct |
18 |
Correct |
457 ms |
6828 KB |
Output is correct |
19 |
Correct |
463 ms |
7180 KB |
Output is correct |
20 |
Correct |
293 ms |
6708 KB |
Output is correct |
21 |
Correct |
436 ms |
7448 KB |
Output is correct |
22 |
Correct |
215 ms |
6996 KB |
Output is correct |
23 |
Correct |
341 ms |
6900 KB |
Output is correct |
24 |
Correct |
1360 ms |
7988 KB |
Output is correct |
25 |
Correct |
789 ms |
8188 KB |
Output is correct |
26 |
Correct |
748 ms |
6788 KB |
Output is correct |
27 |
Correct |
174 ms |
6696 KB |
Output is correct |
28 |
Correct |
1452 ms |
8252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
1 ms |
2664 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2680 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
66 ms |
10408 KB |
Output is correct |
11 |
Correct |
77 ms |
11516 KB |
Output is correct |
12 |
Correct |
53 ms |
11404 KB |
Output is correct |
13 |
Correct |
58 ms |
11820 KB |
Output is correct |
14 |
Correct |
58 ms |
11780 KB |
Output is correct |
15 |
Correct |
229 ms |
6888 KB |
Output is correct |
16 |
Correct |
292 ms |
6696 KB |
Output is correct |
17 |
Correct |
189 ms |
6712 KB |
Output is correct |
18 |
Correct |
189 ms |
6656 KB |
Output is correct |
19 |
Correct |
364 ms |
6724 KB |
Output is correct |
20 |
Correct |
145 ms |
6612 KB |
Output is correct |
21 |
Correct |
297 ms |
6740 KB |
Output is correct |
22 |
Correct |
261 ms |
6732 KB |
Output is correct |
23 |
Correct |
457 ms |
6828 KB |
Output is correct |
24 |
Correct |
463 ms |
7180 KB |
Output is correct |
25 |
Correct |
293 ms |
6708 KB |
Output is correct |
26 |
Correct |
436 ms |
7448 KB |
Output is correct |
27 |
Correct |
215 ms |
6996 KB |
Output is correct |
28 |
Correct |
341 ms |
6900 KB |
Output is correct |
29 |
Correct |
1360 ms |
7988 KB |
Output is correct |
30 |
Correct |
789 ms |
8188 KB |
Output is correct |
31 |
Correct |
748 ms |
6788 KB |
Output is correct |
32 |
Correct |
174 ms |
6696 KB |
Output is correct |
33 |
Correct |
1452 ms |
8252 KB |
Output is correct |
34 |
Correct |
15 ms |
4024 KB |
Output is correct |
35 |
Correct |
310 ms |
11432 KB |
Output is correct |
36 |
Correct |
331 ms |
11440 KB |
Output is correct |
37 |
Correct |
321 ms |
11428 KB |
Output is correct |
38 |
Correct |
342 ms |
11420 KB |
Output is correct |
39 |
Correct |
329 ms |
11416 KB |
Output is correct |
40 |
Correct |
318 ms |
10916 KB |
Output is correct |
41 |
Correct |
360 ms |
12612 KB |
Output is correct |
42 |
Correct |
355 ms |
12292 KB |
Output is correct |
43 |
Correct |
218 ms |
11524 KB |
Output is correct |
44 |
Correct |
381 ms |
12324 KB |
Output is correct |
45 |
Correct |
449 ms |
16852 KB |
Output is correct |
46 |
Correct |
315 ms |
11440 KB |
Output is correct |
47 |
Correct |
344 ms |
11560 KB |
Output is correct |
48 |
Correct |
432 ms |
12536 KB |
Output is correct |
49 |
Correct |
82 ms |
15868 KB |
Output is correct |
50 |
Correct |
83 ms |
12568 KB |
Output is correct |
51 |
Correct |
313 ms |
14080 KB |
Output is correct |
52 |
Correct |
448 ms |
17760 KB |
Output is correct |
53 |
Correct |
429 ms |
18720 KB |
Output is correct |
54 |
Correct |
534 ms |
20276 KB |
Output is correct |
55 |
Correct |
225 ms |
11524 KB |
Output is correct |
56 |
Correct |
669 ms |
18284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2664 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2680 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
66 ms |
10408 KB |
Output is correct |
10 |
Correct |
77 ms |
11516 KB |
Output is correct |
11 |
Correct |
53 ms |
11404 KB |
Output is correct |
12 |
Correct |
58 ms |
11820 KB |
Output is correct |
13 |
Correct |
58 ms |
11780 KB |
Output is correct |
14 |
Correct |
49 ms |
10660 KB |
Output is correct |
15 |
Correct |
132 ms |
15632 KB |
Output is correct |
16 |
Correct |
109 ms |
15380 KB |
Output is correct |
17 |
Correct |
119 ms |
15820 KB |
Output is correct |
18 |
Correct |
128 ms |
15844 KB |
Output is correct |
19 |
Correct |
108 ms |
16892 KB |
Output is correct |
20 |
Correct |
123 ms |
16420 KB |
Output is correct |
21 |
Correct |
118 ms |
16424 KB |
Output is correct |
22 |
Correct |
104 ms |
16312 KB |
Output is correct |
23 |
Correct |
131 ms |
16588 KB |
Output is correct |
24 |
Correct |
108 ms |
16084 KB |
Output is correct |
25 |
Correct |
106 ms |
16244 KB |
Output is correct |
26 |
Correct |
145 ms |
16092 KB |
Output is correct |
27 |
Correct |
229 ms |
6888 KB |
Output is correct |
28 |
Correct |
292 ms |
6696 KB |
Output is correct |
29 |
Correct |
189 ms |
6712 KB |
Output is correct |
30 |
Correct |
189 ms |
6656 KB |
Output is correct |
31 |
Correct |
364 ms |
6724 KB |
Output is correct |
32 |
Correct |
145 ms |
6612 KB |
Output is correct |
33 |
Correct |
297 ms |
6740 KB |
Output is correct |
34 |
Correct |
261 ms |
6732 KB |
Output is correct |
35 |
Correct |
457 ms |
6828 KB |
Output is correct |
36 |
Correct |
15 ms |
4024 KB |
Output is correct |
37 |
Correct |
310 ms |
11432 KB |
Output is correct |
38 |
Correct |
331 ms |
11440 KB |
Output is correct |
39 |
Correct |
321 ms |
11428 KB |
Output is correct |
40 |
Correct |
342 ms |
11420 KB |
Output is correct |
41 |
Correct |
329 ms |
11416 KB |
Output is correct |
42 |
Correct |
318 ms |
10916 KB |
Output is correct |
43 |
Correct |
360 ms |
12612 KB |
Output is correct |
44 |
Correct |
355 ms |
12292 KB |
Output is correct |
45 |
Correct |
218 ms |
11524 KB |
Output is correct |
46 |
Correct |
381 ms |
12324 KB |
Output is correct |
47 |
Execution timed out |
2053 ms |
4308 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
1 ms |
2664 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2680 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
66 ms |
10408 KB |
Output is correct |
11 |
Correct |
77 ms |
11516 KB |
Output is correct |
12 |
Correct |
53 ms |
11404 KB |
Output is correct |
13 |
Correct |
58 ms |
11820 KB |
Output is correct |
14 |
Correct |
58 ms |
11780 KB |
Output is correct |
15 |
Correct |
49 ms |
10660 KB |
Output is correct |
16 |
Correct |
132 ms |
15632 KB |
Output is correct |
17 |
Correct |
109 ms |
15380 KB |
Output is correct |
18 |
Correct |
119 ms |
15820 KB |
Output is correct |
19 |
Correct |
128 ms |
15844 KB |
Output is correct |
20 |
Correct |
108 ms |
16892 KB |
Output is correct |
21 |
Correct |
123 ms |
16420 KB |
Output is correct |
22 |
Correct |
118 ms |
16424 KB |
Output is correct |
23 |
Correct |
104 ms |
16312 KB |
Output is correct |
24 |
Correct |
131 ms |
16588 KB |
Output is correct |
25 |
Correct |
108 ms |
16084 KB |
Output is correct |
26 |
Correct |
106 ms |
16244 KB |
Output is correct |
27 |
Correct |
145 ms |
16092 KB |
Output is correct |
28 |
Correct |
229 ms |
6888 KB |
Output is correct |
29 |
Correct |
292 ms |
6696 KB |
Output is correct |
30 |
Correct |
189 ms |
6712 KB |
Output is correct |
31 |
Correct |
189 ms |
6656 KB |
Output is correct |
32 |
Correct |
364 ms |
6724 KB |
Output is correct |
33 |
Correct |
145 ms |
6612 KB |
Output is correct |
34 |
Correct |
297 ms |
6740 KB |
Output is correct |
35 |
Correct |
261 ms |
6732 KB |
Output is correct |
36 |
Correct |
457 ms |
6828 KB |
Output is correct |
37 |
Correct |
15 ms |
4024 KB |
Output is correct |
38 |
Correct |
310 ms |
11432 KB |
Output is correct |
39 |
Correct |
331 ms |
11440 KB |
Output is correct |
40 |
Correct |
321 ms |
11428 KB |
Output is correct |
41 |
Correct |
342 ms |
11420 KB |
Output is correct |
42 |
Correct |
329 ms |
11416 KB |
Output is correct |
43 |
Correct |
318 ms |
10916 KB |
Output is correct |
44 |
Correct |
360 ms |
12612 KB |
Output is correct |
45 |
Correct |
355 ms |
12292 KB |
Output is correct |
46 |
Correct |
218 ms |
11524 KB |
Output is correct |
47 |
Correct |
381 ms |
12324 KB |
Output is correct |
48 |
Execution timed out |
2053 ms |
4308 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |