Submission #188250

#TimeUsernameProblemLanguageResultExecution timeMemory
188250qkxwsm007 (CEOI14_007)C++14
30 / 100
581 ms22472 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define MAXN 200013
#define INF 1000000007

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M, S, T, A, B;
vi edge[MAXN];
vi ds, dt, da, db;
int dis[MAXN];
int ans;

void bfs(int s, vi &dist)
{
    FOR(i, 0, N) dist[i] = INF;
    queue<int> q; q.push(s); dist[s] = 0;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        for (int v : edge[u])
        {
            if (dist[v] > dist[u] + 1)
            {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
}
bool check(int x)
{
    queue<int> q;
    FOR(i, 0, N)
    {
        if (dt[i] <= x)
        {
            dis[i] = 0;
            q.push(i);
        }
        else dis[i] = INF;
    }
    //they start at T, you start at S.
    //they need to be able to reach A or B at least one second before you.
    // if (dis[A] == 0 || dis[B] == 0) return false;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        for (int v : edge[u])
        {
            //can i go to v?
            if (dis[v] > dis[u] + 1 && ds[v] > dis[u] + 1)
            {
                dis[v] = dis[u] + 1;
                q.push(v);
            }
        }
    }
    return (dis[A] == INF && dis[B] == INF);
    //they can start at any place marked ok.
    //they go, then you go.
}

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> M >> S >> T >> A >> B; S--; T--; A--; B--;
    assert(S != A && S != B && T != A && T != B);
    FOR(i, 0, M)
    {
        int u, v;
        cin >> u >> v;
        u--; v--;
        edge[u].PB(v);
        edge[v].PB(u);
    }
    ds.resize(N); dt.resize(N); da.resize(N); db.resize(N);
    bfs(S, ds);
    bfs(T, dt);
    bfs(A, da);
    bfs(B, db);
    int lo = 0, hi = N + 1;
    while(hi > lo)
    {
        int mid = (hi + lo + 1) >> 1;
        if (check(mid)) lo = mid;
        else hi = mid - 1;
    }
    ans = lo - 1;
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...