답안 #793407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793407 2023-07-25T19:21:11 Z Valaki2 007 (CEOI14_007) C++14
100 / 100
162 ms 17568 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

const int maxn = 2e5;

int n, m;
vector<int> graph[1 + maxn];
int g, s, a, b;
vector<int> dg, ds, da, db;

vector<int> bfs(int start) {
    vector<int> res(1 + n, -1);
    res[start] = 0;
    queue<int> q;
    q.push(start);
    while(!q.empty()) {
        int cur = q.front();
        q.pop();
        for(int nei : graph[cur]) {
            if(res[nei] == -1) {
                res[nei] = res[cur] + 1;
                q.push(nei);
            }
        }
    }
    return res;
}

void solve() {
    cin >> n >> m;
    cin >> g >> s >> a >> b;
    for(int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
    dg = bfs(g), ds = bfs(s), da = bfs(a), db = bfs(b);
    if((da[s] < da[g]) || (db[s] < db[g])) {
        cout << "-1\n";
        return;
    }
    int add_a = da[s] - 1 - da[g], add_b = db[s] - 1 - db[g];
    bool extra_case = true;
    if(add_a != add_b) {
        add_a++, add_b++;
        extra_case = false;
    }
    int to_add = min(add_a, add_b);
    if(!extra_case) {
        cout << to_add << "\n";
        return;
    }
    to_add++;
    int dag0 = da[g], dbg0 = db[g];
    int closest_g = max(dag0, dbg0), closest_s = n + 1;
    da[g] += to_add;
    db[g] += to_add;
    for(int i = 1; i <= n; i++) {
        if(/*i != g*/true) {
            dg[i] += to_add;
        }
    }
    if(da[s] != db[s] || da[g] != db[g]) {
        //exit(1);
    }
    if(da[s] != da[g] || db[s] != db[g]) {
        exit(1);
    }
    for(int cur = 1; cur <= n; cur++) {
        /*int max_dab = max(da[cur], db[cur]), min_dab = min(da[cur], db[cur]);
        if((max_dab + closest_g == max(da[g], db[g])) && (min_dab + closest_g == min(da[g], db[g]))) {
            closest_g = min(closest_g, max_dab);
        }
        if((max_dab + closest_s == max(da[s], db[s]))) {
            closest_s = min(closest_s, max_dab);
        }*/
        int max_dab = max(da[cur], db[cur]);
        int min_dab = min(da[cur], db[cur]);
        if(cur == g) {
            max_dab = max(dag0, dbg0);
            min_dab = min(dag0, dbg0);
        }
        if(cur != g) {
            if(max_dab + dg[cur] == max(da[g], db[g])) {
                if(min_dab + dg[cur] == min(da[g], db[g])) {
                    closest_g = min(closest_g, max_dab);
                }
            }
        }
        if(max_dab + ds[cur] == max(da[s], db[s])) {
            if(min_dab + ds[cur] == min(da[s], db[s])) {
                closest_s = min(closest_s, max_dab);
            }
        }
    }
    if(closest_s < closest_g) {
        to_add--;
    }
    cout << to_add << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 2 ms 4948 KB Output is correct
24 Correct 2 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 6888 KB Output is correct
2 Correct 16 ms 7800 KB Output is correct
3 Correct 15 ms 6996 KB Output is correct
4 Correct 18 ms 7808 KB Output is correct
5 Correct 18 ms 6744 KB Output is correct
6 Correct 13 ms 7124 KB Output is correct
7 Correct 18 ms 7248 KB Output is correct
8 Correct 16 ms 7252 KB Output is correct
9 Correct 24 ms 7764 KB Output is correct
10 Correct 87 ms 12112 KB Output is correct
11 Correct 29 ms 9188 KB Output is correct
12 Correct 38 ms 10232 KB Output is correct
13 Correct 43 ms 9584 KB Output is correct
14 Correct 24 ms 8788 KB Output is correct
15 Correct 36 ms 10436 KB Output is correct
16 Correct 36 ms 10668 KB Output is correct
17 Correct 32 ms 10068 KB Output is correct
18 Correct 41 ms 10092 KB Output is correct
19 Correct 57 ms 11340 KB Output is correct
20 Correct 118 ms 14196 KB Output is correct
21 Correct 59 ms 12492 KB Output is correct
22 Correct 50 ms 11508 KB Output is correct
23 Correct 46 ms 12376 KB Output is correct
24 Correct 50 ms 12300 KB Output is correct
25 Correct 67 ms 12148 KB Output is correct
26 Correct 49 ms 11580 KB Output is correct
27 Correct 72 ms 12548 KB Output is correct
28 Correct 55 ms 12492 KB Output is correct
29 Correct 77 ms 12900 KB Output is correct
30 Correct 123 ms 14924 KB Output is correct
31 Correct 56 ms 13644 KB Output is correct
32 Correct 51 ms 12388 KB Output is correct
33 Correct 49 ms 12620 KB Output is correct
34 Correct 65 ms 13088 KB Output is correct
35 Correct 49 ms 12772 KB Output is correct
36 Correct 56 ms 13048 KB Output is correct
37 Correct 59 ms 14068 KB Output is correct
38 Correct 72 ms 13924 KB Output is correct
39 Correct 79 ms 13896 KB Output is correct
40 Correct 105 ms 15492 KB Output is correct
41 Correct 162 ms 17568 KB Output is correct