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;
#define N 200010
#define ll long long
#define ii pair<int,int>
#define fs first
#define sc second
#define pb push_back
int dp[N][5],f[5],n,m;
vector<int>g[N];
deque<int>dq;
void build(int id)
{
dp[f[id]][id]=0;
dq.pb(f[id]);
while(!dq.empty())
{
int u=dq.front();
dq.pop_front();
for(auto v:g[u])
if(dp[v][id]>dp[u][id]+1)
{
dp[v][id]=dp[u][id]+1;
dq.pb(v);
}
}
}
int main()
{
// freopen("A.inp","r",stdin);
// freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>f[1]>>f[2]>>f[3]>>f[4];
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
memset(dp,0x3f3f,sizeof(dp));
for(int i=1;i<=2;i++)
build(i);
int val=min(dp[f[3]][2]-dp[f[3]][1],dp[f[4]][2]-dp[f[4]][1]);
if(val<0)
cout<<-1;
else cout<<val;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |