This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,m,s,d,a,b;
vector<int> muchii[200005];
int dist[6][200005];
bool use[200005];
void bfs(int start,int index)
{
for(int i=1;i<=n;i++)
dist[index][i]=1e9;
dist[index][start]=0;
queue<int> coada;
coada.push(start);
while(!coada.empty())
{
int nod=coada.front();
coada.pop();
for(int i:muchii[nod])
if(dist[index][i]>dist[index][nod]+1)
{
dist[index][i]=dist[index][nod]+1;
coada.push(i);
}
}
}
void dfs(int nod,int bad)
{
use[nod]=1;
for(int i:muchii[nod])
if(!use[i]&&i!=bad)
dfs(i,bad);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>s>>d>>a>>b;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
bfs(a,1);
bfs(b,2);
bfs(d,3);
bfs(s,4);
int minim=1e9;
for(int i=1;i<=n;i++)
{
bool good;
if((dist[1][i]>=dist[1][s]&&dist[2][i]>=dist[2][s])||dist[4][i]<=1)
good=1;
else
{
bfs(i,5);
good=0;
for(int j=1;j<=n;j++)
if(j!=i)
{
for(int k=1;k<=n;k++)
use[k]=0;
dfs(i,j);
if(use[a]||use[b])
continue;
if(dist[4][j]<=dist[5][j]+1)
good=1;
break;
}
}
if(!good)
minim=min(minim,dist[3][i]-2);
}
cout<<max(-1,minim);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |