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;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)2e5 + 10;
vector<int> T[N];
int c = 0;
int d[4][N];
int n;
void bfs(int node){
for(int i = 1; i <= n; i ++ )
d[c][i] = (int)1e9;
queue<int> bf;
bf.push(node);
d[c][node] = 0;
while(!bf.empty()){
node = bf.front();
bf.pop();
for(auto x : T[node]){
if(d[c][x] > d[c][node] + 1){
d[c][x] = d[c][node] + 1;
bf.push(x);
}
}
}
c ++ ;
}
int main(){
fastIO;
int m;
cin >> n >> m;
int a, b;
cin >> a >> b;
int n0, n1;
cin >> n0 >> n1;
int u, v;
for(int i = 0 ; i < m ; i ++ ){
cin >> u >> v;
T[u].push_back(v);
T[v].push_back(u);
}
bfs(a);
bfs(b);
bfs(n0);
bfs(n1);
int d0 = d[1][n0] - d[0][n0];
int d1 = d[1][n1] - d[0][n1];
cout << max(-1,min(d0,d1)-1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |