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 ;
const int inf = 1e9 ;
const int MAX = 2e5 + 10 ;
int arr[MAX] ;
int n , m ;
int a , b , x , y ;
vector< vector<int> >adj(MAX) ;
int dist[4][MAX] ;
void bfs(int src , int t)
{
for(int i = 1 ; i <= n ; ++i)
dist[t][i] = inf ;
queue<int>q ;
dist[t][src] = 0 ;
q.push(src) ;
while(!q.empty())
{
int node = q.front() ;
q.pop() ;
for(auto &child : adj[node])
{
if(dist[t][node]+1 < dist[t][child])
{
dist[t][child] = dist[t][node]+1 ;
q.push(child) ;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
cin>>a>>b>>x>>y ;
for(int i = 0 ; i < m ; ++i)
{
int u , v ;
cin>>u>>v ;
adj[u].push_back(v) ;
adj[v].push_back(u) ;
}
bfs(a , 0) ;
bfs(b , 1) ;
bfs(x , 2) ;
bfs(y , 3) ;
if(dist[1][x] < dist[0][x] || dist[1][y] < dist[0][y])
return cout<<-1<<"\n" , 0 ;
if(dist[1][x] - dist[0][x] != dist[1][y] - dist[0][y])
return cout<<min(dist[1][x] - dist[0][x] , dist[1][y] - dist[0][y]) , 0 ;
int Min1 = inf , Min2 = inf ;
for(int i = 1 ; i <= n ; ++i)
{
if(dist[0][i] + dist[2][i] == dist[0][x] && dist[0][i] + dist[3][i] == dist[0][y])
Min1 = min(Min1 , dist[2][i]) ;
if(dist[1][i] + dist[2][i] == dist[1][x] && dist[1][i] + dist[3][i] == dist[1][y])
Min2 = min(Min2 , dist[2][i]) ;
}
if(Min1 <= Min2)
cout<<dist[1][x] - dist[0][x]<<"\n" ;
else
cout<<dist[1][x] - dist[0][x] - 1<<"\n" ;
return 0 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |