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 bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
typedef pair<int, int> ii;
const int N = 2e5 + 5;
int n, m, s, d, a, b, dist[N];
vector<int> g[N];
ii bfs(int st) {
memset(dist, 63, sizeof dist);
queue<int> q;
q.push(st);
dist[st] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
fa (v, g[u]) if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
return ii(dist[a], dist[b]);
}
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> s >> d >> a >> b;
s--, d--, a--, b--;
fw (i, 0, m) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].pb(v), g[v].pb(u);
}
ii dists = bfs(s), distd = bfs(d);
int sa = dists.fi, sb = dists.se, da = distd.fi, db = distd.se;
int tmp1 = da - sa, tmp2 = db - sb;
int ans = min(tmp1, tmp2);
if (ans < 0) cout << "-1";
else cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |