Submission #442603

#TimeUsernameProblemLanguageResultExecution timeMemory
442603prvocislo007 (CEOI14_007)C++17
100 / 100
294 ms18024 KiB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
int n, m, s, d, a, b;
vector<int> g[maxn], ds(maxn, -1), dd(maxn, -1), da(maxn, -1), db(maxn, -1);
void bfs(int st, vector<int> &d)
{
    queue<int> q;
    d[st] = 0; q.push(st);
    while (!q.empty())
    {
        int u = q.front(); q.pop();
        for (int v : g[u]) if (d[v] == -1) d[v] = d[u]+1, q.push(v);
    }
}
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);
    bfs(s, ds), bfs(d, dd), bfs(a, da), bfs(b, db);
    int wa = dd[a] - ds[a], wb = dd[b] - ds[b];
    int ans;
    if (wa < wb) ans = wa;
    else if (wa > wb) ans = wb;
    else 
    {
        int s2 = 0, d2 = 0;
        for (int i = 0; i < n; i++) 
            if (ds[i]+da[i] == ds[a] && ds[i]+db[i] == ds[b]) 
                s2 = max(s2, ds[i]);
        for (int i = 0; i < n; i++) 
            if (dd[i]+da[i] == dd[a] && dd[i]+db[i] == dd[b]) 
                d2 = max(d2, dd[i]);
        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...