답안 #793382

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
793382 2023-07-25T18:51:55 Z Valaki2 007 (CEOI14_007) C++14
0 / 100
167 ms 18124 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++;
    da[s] += to_add;
    db[s] += to_add;
    int closest_g = n + 1, closest_s = n + 1;
    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 = max(da[cur], db[cur]);
        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 + dg[cur] == max(da[s], db[s])) {
            if(min_dab + dg[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 3 ms 4948 KB Output is correct
2 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
3 Correct 2 ms 5020 KB Output is correct
4 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
5 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
6 Runtime error 3 ms 5024 KB Execution failed because the return code was nonzero
7 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
8 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
9 Runtime error 3 ms 4972 KB Execution failed because the return code was nonzero
10 Correct 2 ms 5024 KB Output is correct
11 Correct 2 ms 5024 KB Output is correct
12 Runtime error 3 ms 5016 KB Execution failed because the return code was nonzero
13 Runtime error 3 ms 4948 KB Execution failed because the return code was nonzero
14 Runtime error 3 ms 4948 KB Execution failed because the return code was nonzero
15 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
16 Runtime error 3 ms 4948 KB Execution failed because the return code was nonzero
17 Runtime error 3 ms 5044 KB Execution failed because the return code was nonzero
18 Runtime error 3 ms 4948 KB Execution failed because the return code was nonzero
19 Runtime error 2 ms 5020 KB Execution failed because the return code was nonzero
20 Runtime error 3 ms 4960 KB Execution failed because the return code was nonzero
21 Correct 3 ms 5028 KB Output is correct
22 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
23 Runtime error 2 ms 4948 KB Execution failed because the return code was nonzero
24 Runtime error 3 ms 5076 KB Execution failed because the return code was nonzero
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 7296 KB Execution failed because the return code was nonzero
2 Runtime error 23 ms 8184 KB Execution failed because the return code was nonzero
3 Runtime error 20 ms 7304 KB Execution failed because the return code was nonzero
4 Runtime error 19 ms 8348 KB Execution failed because the return code was nonzero
5 Correct 17 ms 7124 KB Output is correct
6 Correct 14 ms 7452 KB Output is correct
7 Runtime error 20 ms 7716 KB Execution failed because the return code was nonzero
8 Runtime error 17 ms 7764 KB Execution failed because the return code was nonzero
9 Runtime error 26 ms 8472 KB Execution failed because the return code was nonzero
10 Runtime error 93 ms 12856 KB Execution failed because the return code was nonzero
11 Runtime error 30 ms 9928 KB Execution failed because the return code was nonzero
12 Runtime error 41 ms 11028 KB Execution failed because the return code was nonzero
13 Runtime error 34 ms 10292 KB Execution failed because the return code was nonzero
14 Correct 26 ms 9608 KB Output is correct
15 Runtime error 52 ms 11200 KB Execution failed because the return code was nonzero
16 Correct 50 ms 11496 KB Output is correct
17 Runtime error 33 ms 10844 KB Execution failed because the return code was nonzero
18 Runtime error 39 ms 10836 KB Execution failed because the return code was nonzero
19 Runtime error 66 ms 12108 KB Execution failed because the return code was nonzero
20 Runtime error 122 ms 14928 KB Execution failed because the return code was nonzero
21 Runtime error 59 ms 13328 KB Execution failed because the return code was nonzero
22 Runtime error 48 ms 12184 KB Execution failed because the return code was nonzero
23 Runtime error 50 ms 13136 KB Execution failed because the return code was nonzero
24 Runtime error 48 ms 13152 KB Execution failed because the return code was nonzero
25 Runtime error 46 ms 12624 KB Execution failed because the return code was nonzero
26 Runtime error 45 ms 12316 KB Execution failed because the return code was nonzero
27 Runtime error 51 ms 13280 KB Execution failed because the return code was nonzero
28 Runtime error 64 ms 13260 KB Execution failed because the return code was nonzero
29 Runtime error 108 ms 13688 KB Execution failed because the return code was nonzero
30 Runtime error 155 ms 15660 KB Execution failed because the return code was nonzero
31 Runtime error 59 ms 14360 KB Execution failed because the return code was nonzero
32 Runtime error 62 ms 13120 KB Execution failed because the return code was nonzero
33 Runtime error 52 ms 13468 KB Execution failed because the return code was nonzero
34 Runtime error 73 ms 13836 KB Execution failed because the return code was nonzero
35 Runtime error 55 ms 13516 KB Execution failed because the return code was nonzero
36 Runtime error 66 ms 13920 KB Execution failed because the return code was nonzero
37 Correct 64 ms 14752 KB Output is correct
38 Runtime error 63 ms 14580 KB Execution failed because the return code was nonzero
39 Runtime error 66 ms 14580 KB Execution failed because the return code was nonzero
40 Runtime error 123 ms 16048 KB Execution failed because the return code was nonzero
41 Runtime error 167 ms 18124 KB Execution failed because the return code was nonzero