답안 #830098

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
830098 2023-08-18T18:49:11 Z t6twotwo Mousetrap (CEOI17_mousetrap) C++17
25 / 100
846 ms 88176 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, t, m;
    cin >> N >> t >> m;
    t--, m--;
    vector<vector<int>> adj(N);
    for (int i = 0; i < N - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    vector<int> p(N, -1);
    queue<int> q;
    p[m] = m;
    q.push(m);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int y : adj[x]) {
            if (p[y] == -1) {
                p[y] = x;
                q.push(y);
            }
        }
    }
    vector<int> path{t};
    while (path.back() != m) {
        path.push_back(p[path.back()]);
    }
    vector<bool> in(N);
    for (int x : path) {
        in[x] = 1;
    }
    vector<int> f(N);
    auto dfs = [&](auto dfs, int x, int p) -> void {
        vector<int> s;
        for (int y : adj[x]) {
            if (y == p || in[y]) {
                continue;
            }
            dfs(dfs, y, x);
            s.push_back(f[y]);
        }
        sort(s.rbegin(), s.rend());
        if (s.empty()) {
            return;
        }
        if (s.size() == 1) {
            f[x] = 1;
            return;
        }
        f[x] = s[1] + s.size();
    };
    vector<vector<int>> ch(N);
    for (int x : path) {
        for (int y : adj[x]) {
            if (!in[y]) {
                ch[x].push_back(y);
            }
        }
        dfs(dfs, x, -1);
    }
    int M = path.size();
    vector<int> z(M + 1);
    for (int i = 0; i < M; i++) {
        z[i + 1] = z[i] + ch[path[i]].size();
    }
    vector<int> dp(M, 1E9);
    dp[1] = f[path[1]];
    for (int i = 2; i < M; i++) {
        vector<int> s;
        for (int j : ch[path[i]]) {
            s.push_back(f[j]);
        }
        sort(s.rbegin(), s.rend());
        if (s.empty()) {
            dp[i] = dp[i - 1];
        } else {
            dp[i] = max(dp[i - 1], s[0] + z[i + 1]);
            if (s.size() == 1) {
                dp[i] = min(dp[i], dp[i - 1] + 1);
            } else {
                dp[i] = min(dp[i], max(dp[i - 1] + 1, s[1] + z[i + 1]));
            }
        }
    }
    cout << dp[M - 1] << "\n";
    return 6/22;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 88176 KB Output is correct
2 Correct 243 ms 79672 KB Output is correct
3 Correct 846 ms 88044 KB Output is correct
4 Correct 316 ms 44104 KB Output is correct
5 Correct 778 ms 88028 KB Output is correct
6 Correct 796 ms 87932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -