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 = 200005;
int n, m, s, d, a, b;
vector<int> G[N];
int fa[N], fb[N];
void bfs(int start, int f[]) {
for (int i = 1; i <= n; ++i) f[i] = -1;
f[start] = 0;
queue <int> q; q.push(start);
while(!q.empty()) {
int u = q.front(); q.pop();
for (int v : G[u]) if (f[v] == -1) {
f[v] = f[u] + 1; q.push(v);
}
}
}
int maxdep(int start) {
vector<int> d(n + 1, -1);
queue <int> q; q.push(start); d[start] = 0;
int ans = 0;
while(!q.empty()) {
int u = q.front(); q.pop();
ans = max(ans, d[u]);
for (int v : G[u]) if (fa[v] == fa[u] - 1 && fb[v] == fb[u] - 1 && d[v] == -1) {
d[v] = d[u] + 1; q.push(v);
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
cin >> s >> d >> a >> b;
while(m--) {
int u, v; cin >> u >> v;
G[u].push_back(v); G[v].push_back(u);
}
bfs(a, fa);
bfs(b, fb);
int wa = fa[d] - fa[s];
int wb = fb[d] - fb[s];
if (wa != wb) {
printf("%d\n", max(min(wa, wb), -1));
} else {
int maxs = maxdep(s), maxd = maxdep(d);
printf("%d\n", max(wa - (wa + maxs < maxd), -1));
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |