이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, m, s, d, a, b;
vector<int> g[maxn];
void bfs(int st, int &d1, int &d2, int &both)
{
queue<int> q;
vector<int> d(n, -1), fa(n, -1);
d[st] = 0; q.push(st);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int v : g[u])
{
if (d[v] != -1) continue;
d[v] = d[u]+1, fa[v] = u;
q.push(v);
}
}
d1 = d[a], d2 = d[b];
vector<int> p1, p2;
for (int i = a; i != -1; i = fa[i]) p1.push_back(i);
for (int i = b; i != -1; i = fa[i]) p2.push_back(i);
both = 0;
while (p1.size() && p2.size() && p1.back() == p2.back()) both++, p1.pop_back(), p2.pop_back();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> s >> d >> a >> b;
s--, d--, a--, b--;
for (int i = 0, u, v; i < m; i++) cin >> u >> v, g[--u].push_back(--v), g[v].push_back(u);
int sa, sb, s2, da, db, d2;
bfs(s, sa, sb, s2), bfs(d, da, db, d2);
int wa = da - sa, wb = db - sb;
int ans;
if (wa < wb) ans = wa;
else if (wa > wb) ans = wb;
else if (s2 + wa >= d2) ans = wa;
else ans = wa - 1;
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... |