답안 #958514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958514 2024-04-06T00:11:21 Z Ghetto 007 (CEOI14_007) C++17
100 / 100
396 ms 28368 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 2e5 + 5, INF = 1e9;

int n, m;
int a, b, tar1, tar2;
vector<int> adj[MAX_N];

int dist[MAX_N];
bool seen[MAX_N];
queue<int> order;
void bfs(int s) {
    for (int i = 1; i <= n; i++) seen[i] = false;

    seen[s] = true;
    dist[s] = 0;
    order.push(s);
    while (order.size()) {
        int u = order.front(); 
        order.pop();

        for (int v : adj[u]) {
            if (seen[v]) continue;
            seen[v] = true;
            dist[v] = dist[u] + 1;
            order.push(v);
        }
    }
}

int d[MAX_N][3];
int dp[MAX_N];
vector<pii> order2;
void precomp() {
    bfs(tar1);
    for (int i = 1; i <= n; i++) d[i][1] = dist[i];
    bfs(tar2);
    for (int i = 1; i <= n; i++) d[i][2] = dist[i];

    for (int i = 1; i <= n; i++) {
        if (d[i][1] != d[i][2]) continue;
        order2.push_back({d[i][1], i});
    }
    sort(order2.begin(), order2.end());

    for (pii el : order2) {
        int u = el.second;
        dp[u] = 1;
        for (int v : adj[u]) {
            if (!(d[v][1] == d[u][1] - 1 && d[v][2] == d[u][2] - 1)) continue;
            dp[u] = max(dp[u], dp[v] + 1);
        }
    }

    for (int i = 1; i <= n; i++) {
        // cout << i << ": " << d[i][1] << " " << d[i][2] << endl;
        // cout << i << ": " << dp[i] << endl;
    }
}

bool does_b_win(int new_b) {
    if (d[a][1] > d[new_b][1] || d[a][2] > d[new_b][2]) return true;
    if (d[a][1] == d[new_b][1] && d[a][2] == d[new_b][2] && d[a][1] == d[a][2] && dp[a] < dp[new_b]) return true;
    return false;
}

int main() {
    // freopen("007.in", "r", stdin);

    cin >> n >> m;
    cin >> a >> b >> tar1 >> tar2;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    precomp();

    bfs(b);
    int ans = INF;
    for (int i = 1; i <= n; i++) {
        if (!does_b_win(i)) continue;
        ans = min(ans, dist[i]);
    }
    ans--;
    cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 2 ms 9224 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9048 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9052 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 2 ms 9052 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 3 ms 9076 KB Output is correct
13 Correct 2 ms 9048 KB Output is correct
14 Correct 2 ms 9048 KB Output is correct
15 Correct 2 ms 9052 KB Output is correct
16 Correct 2 ms 9052 KB Output is correct
17 Correct 2 ms 9048 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 2 ms 9052 KB Output is correct
20 Correct 2 ms 9052 KB Output is correct
21 Correct 2 ms 9048 KB Output is correct
22 Correct 2 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 2 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 11056 KB Output is correct
2 Correct 35 ms 11856 KB Output is correct
3 Correct 27 ms 11100 KB Output is correct
4 Correct 35 ms 11960 KB Output is correct
5 Correct 25 ms 10836 KB Output is correct
6 Correct 24 ms 10836 KB Output is correct
7 Correct 28 ms 11148 KB Output is correct
8 Correct 29 ms 11344 KB Output is correct
9 Correct 65 ms 12464 KB Output is correct
10 Correct 219 ms 21212 KB Output is correct
11 Correct 59 ms 13396 KB Output is correct
12 Correct 72 ms 14148 KB Output is correct
13 Correct 60 ms 13356 KB Output is correct
14 Correct 52 ms 12628 KB Output is correct
15 Correct 68 ms 14424 KB Output is correct
16 Correct 74 ms 14336 KB Output is correct
17 Correct 65 ms 13904 KB Output is correct
18 Correct 63 ms 13796 KB Output is correct
19 Correct 136 ms 17240 KB Output is correct
20 Correct 261 ms 22988 KB Output is correct
21 Correct 96 ms 16476 KB Output is correct
22 Correct 105 ms 15568 KB Output is correct
23 Correct 104 ms 15884 KB Output is correct
24 Correct 93 ms 16208 KB Output is correct
25 Correct 109 ms 15984 KB Output is correct
26 Correct 83 ms 14996 KB Output is correct
27 Correct 97 ms 15952 KB Output is correct
28 Correct 100 ms 16212 KB Output is correct
29 Correct 175 ms 20348 KB Output is correct
30 Correct 274 ms 24080 KB Output is correct
31 Correct 113 ms 17552 KB Output is correct
32 Correct 99 ms 16340 KB Output is correct
33 Correct 111 ms 16244 KB Output is correct
34 Correct 108 ms 17072 KB Output is correct
35 Correct 112 ms 16652 KB Output is correct
36 Correct 107 ms 17108 KB Output is correct
37 Correct 137 ms 17748 KB Output is correct
38 Correct 152 ms 17232 KB Output is correct
39 Correct 122 ms 17560 KB Output is correct
40 Correct 242 ms 23120 KB Output is correct
41 Correct 396 ms 28368 KB Output is correct