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 finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 1;
const int OOO = 1;
using namespace std;
int n, m;
int st7, stn, a, b;
vector<vector<int>> g;
vector<int> d;
void bfs(int st) {
for (auto &i : d) i = -1;
d[st] = 0;
queue<int> q;
q.push(st);
while (q.size()) {
int x = q.front();
q.pop();
for (const auto &i : g[x])
if (d[i] == -1) {
d[i] = 1 + d[x];
q.push(i);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> st7 >> stn >> a >> b;
--st7, --stn, --a, --b;
g.resize(n);
d.resize(n);
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
bfs(st7);
int D7[2] = { d[a], d[b] };
bfs(stn);
int Dn[2] = { d[a], d[b] };
int ans = min(Dn[0] - D7[0], Dn[1] - D7[1]);
cout << max(ans, -1) << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |