제출 #442597

#제출 시각아이디문제언어결과실행 시간메모리
442597prvocislo007 (CEOI14_007)C++17
90 / 100
262 ms23500 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...