Submission #52341

# Submission time Handle Problem Language Result Execution time Memory
52341 2018-06-25T13:02:22 Z Alexa2001 007 (CEOI14_007) C++17
100 / 100
330 ms 16556 KB
#include <bits/stdc++.h>

using namespace std;
/// 15:26

const int Nmax = 2e5 + 5, inf = 2e9;

int n, m, A, B, S, D, i, x, y;
int dist1[Nmax], dist2[Nmax];
vector<int> v[Nmax];

bool adv(int &node)
{
    for(auto it : v[node])
        if(dist1[it] == dist1[node] - 1 && dist2[it] == dist2[node] - 1)
        {
            node = it;
            return 1;
        }
    return 0;
}

bool win(int tmp)
{
    int a, b, c, d;
    a = dist1[D], b = dist2[D];
    c = dist1[S] + tmp, d = dist2[S] + tmp;

    if(a < c || b < d) return 0;
    if(c < a && c < b) return 1;
    if(d < a && d < b) return 1;
    if(a == c && b > d) return 1;
    if(b == d && a > c) return 1;

    /// pana aici totul e frumos, iese pe hartie
    /// si acum vine cazul naspa, cireasa de pe tort
    /// avem a = c si b = d
    /// trebuie sa vedem care dintre aia doi ajunge primul la un nod care nu mai are continuare echidistanta fata de A si B
    /// cred ca nu se intersecteaza pe parcurs, cred...

    assert(a==c && b==d); /// sa ma aigur ca totusi n-am scapat ceva

    int i, ss = S, dd = D;
    for(i=1; i<=a; ++i)
    {
        if(!adv(dd)) return 1;
        if(i<=tmp) continue;
        if(!adv(ss)) return 0;
    }

    return 1;
}

int solve()
{
    int st = 0, dr = n, mid;
    while(st <= dr)
    {
        mid = (st+dr)/2;
        if(!win(mid)) dr = mid-1;
            else st = mid+1;
    }
    return dr;
}

void calc(int node, int d[])
{
    int i;
    for(i=1; i<=n; ++i) d[i] = inf;

    queue<int> q;
    d[node] = 0;
    q.push(node);

    while(q.size())
    {
        node = q.front();
        q.pop();
        for(auto it : v[node])
            if(d[it] == inf)
            {
                d[it] = d[node] + 1;
                q.push(it);
            }
    }
}

int main()
{
  //  freopen("input", "r", stdin);
  //  freopen("output", "w", stdout);
    cin.sync_with_stdio(false);

    cin >> n >> m;
    cin >> S >> D >> A >> B;

    for(i=1; i<=m; ++i)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    calc(A, dist1);
    calc(B, dist2);

    cout << solve() << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4984 KB Output is correct
2 Correct 7 ms 5104 KB Output is correct
3 Correct 7 ms 5104 KB Output is correct
4 Correct 5 ms 5132 KB Output is correct
5 Correct 5 ms 5132 KB Output is correct
6 Correct 6 ms 5196 KB Output is correct
7 Correct 6 ms 5224 KB Output is correct
8 Correct 6 ms 5368 KB Output is correct
9 Correct 5 ms 5368 KB Output is correct
10 Correct 5 ms 5368 KB Output is correct
11 Correct 5 ms 5368 KB Output is correct
12 Correct 6 ms 5368 KB Output is correct
13 Correct 6 ms 5368 KB Output is correct
14 Correct 6 ms 5368 KB Output is correct
15 Correct 6 ms 5368 KB Output is correct
16 Correct 6 ms 5376 KB Output is correct
17 Correct 6 ms 5376 KB Output is correct
18 Correct 7 ms 5376 KB Output is correct
19 Correct 6 ms 5376 KB Output is correct
20 Correct 6 ms 5376 KB Output is correct
21 Correct 6 ms 5376 KB Output is correct
22 Correct 6 ms 5376 KB Output is correct
23 Correct 7 ms 5376 KB Output is correct
24 Correct 6 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 6908 KB Output is correct
2 Correct 34 ms 7676 KB Output is correct
3 Correct 27 ms 7676 KB Output is correct
4 Correct 35 ms 7676 KB Output is correct
5 Correct 26 ms 7676 KB Output is correct
6 Correct 28 ms 7676 KB Output is correct
7 Correct 30 ms 7676 KB Output is correct
8 Correct 31 ms 7676 KB Output is correct
9 Correct 49 ms 7676 KB Output is correct
10 Correct 211 ms 12016 KB Output is correct
11 Correct 78 ms 12016 KB Output is correct
12 Correct 78 ms 12016 KB Output is correct
13 Correct 62 ms 12016 KB Output is correct
14 Correct 52 ms 12016 KB Output is correct
15 Correct 96 ms 12016 KB Output is correct
16 Correct 85 ms 12016 KB Output is correct
17 Correct 71 ms 12016 KB Output is correct
18 Correct 120 ms 12016 KB Output is correct
19 Correct 167 ms 12016 KB Output is correct
20 Correct 284 ms 13564 KB Output is correct
21 Correct 167 ms 13564 KB Output is correct
22 Correct 126 ms 13564 KB Output is correct
23 Correct 109 ms 13564 KB Output is correct
24 Correct 163 ms 13564 KB Output is correct
25 Correct 112 ms 13564 KB Output is correct
26 Correct 94 ms 13564 KB Output is correct
27 Correct 118 ms 13564 KB Output is correct
28 Correct 125 ms 13564 KB Output is correct
29 Correct 171 ms 13564 KB Output is correct
30 Correct 240 ms 14316 KB Output is correct
31 Correct 124 ms 14316 KB Output is correct
32 Correct 126 ms 14316 KB Output is correct
33 Correct 177 ms 14316 KB Output is correct
34 Correct 179 ms 14316 KB Output is correct
35 Correct 166 ms 14316 KB Output is correct
36 Correct 170 ms 14316 KB Output is correct
37 Correct 136 ms 14316 KB Output is correct
38 Correct 143 ms 14316 KB Output is correct
39 Correct 187 ms 14316 KB Output is correct
40 Correct 215 ms 14332 KB Output is correct
41 Correct 330 ms 16556 KB Output is correct