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>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define printv(a, b) {\
for(auto pv : a) b << pv << " ";\
b << "\n";\
}
using namespace std;
using pii = pair<int, int>;
ostream& operator<<(ostream& o, pii p){
return o << '(' << p.F << ',' << p.S << ')';
}
int n, m;
vector<vector<int>> g;
vector<int> getdis(int s){
vector<int> dis(n + 1, -1);
dis[s] = 0;
queue<int> q;
q.push(s);
while(!q.empty()){
int now = q.front();
q.pop();
for(int i : g[now]){
if(dis[i] != -1) continue;
dis[i] = dis[now] + 1;
q.push(i);
}
}
return dis;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int s, d, a, b;
cin >> s >> d >> a >> b;
g.resize(n + 1);
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
vector<int> ds = getdis(s);
vector<int> dd = getdis(d);
vector<int> da = getdis(a);
vector<int> db = getdis(b);
int tmp = min(dd[a], dd[b]);
int ans = ds[a];
for(int i = 1; i <= n; i++){
if(da[i] <= 1 && db[i] <= 1){
ans = min(ds[i], ans);
}
}
ans = tmp - 1 - ans;
ans = max(ans, -1);
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |