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 N = 2e5 + 5;
int dis[4][N];
vector <int> g[N];
void BFS(int v, int *dis) {
fill(dis, dis + N, N);
dis[v] = 0;
queue <int> q; q.push(v);
while (!q.empty()) {
v = q.front(), q.pop();
for (int u : g[v]) if (dis[v] + 1 < dis[u]) dis[u] = dis[v] + 1, q.push(u);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m; cin >> n >> m;
int t, k, a, b; cin >> k >> t >> a >> b;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
BFS(t, dis[0]), BFS(k, dis[1]), BFS(a, dis[2]), BFS(b, dis[3]);
int ans1 = dis[0][a] - dis[1][a], ans2 = dis[0][b] - dis[1][b];
if (min(ans1, ans2) < 0) return cout << -1 << "\n", 0;
if (ans1 != ans2) return cout << min(ans1, ans2) << "\n", 0;
int mx0 = 0, mx1 = 0;
for (int i = 1; i <= n; i++)
if (dis[0][i] + dis[2][i] == dis[0][a] && dis[0][i] + dis[3][i] == dis[0][b]) mx0 = max(mx0, dis[0][i]);
for (int i = 1; i <= n; i++)
if (dis[1][i] + dis[2][i] == dis[1][a] && dis[1][i] + dis[3][i] == dis[1][b]) mx1 = max(mx1, dis[1][i]);
cout << ans1 - (ans1 + mx1 < mx0) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |