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];
if(d0 != d1){
int low = min(d0,d1);
if(low < 0) cout << -1 << "\n";
else cout << low << "\n";
return 0;
}
int cc = 0, dd = 0;
for(int i = 1; i <= n; i ++ ){
if(d[0][i] + d[2][i] == d[0][n0] && d[0][i] + d[3][i] == d[0][n1]){
cc = max(cc, d[0][i]);
}
if(d[1][i] + d[2][i] == d[1][n0] && d[1][i] + d[3][i] == d[1][n1]){
dd = max(dd, d[1][i]);
}
}
if(d0 < 0){
cout << -1 << "\n";
return 0;
}
if(cc + d0 >= dd){
cout << d0 << "\n";
return 0;
}
cout << d0-1 << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |