Submission #856019

# Submission time Handle Problem Language Result Execution time Memory
856019 2023-10-02T14:33:16 Z Cyanmond Mousetrap (CEOI17_mousetrap) C++17
45 / 100
777 ms 72128 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define per(i, l, r) for (int i = (r - 1); i >= l; --i)
#define ALL(x) (x).begin(), (x).end()

using i64 = long long;

void main_() {
    int N, T, M;
    cin >> N >> T >> M;
    --T, --M;
    if (T == M) {
        cout << 0 << endl;
        return;
    }

    vector<int> A(N - 1), B(N - 1);
    vector<vector<int>> Tree(N);
    rep(i, 0, N - 1) {
        cin >> A[i] >> B[i];
        Tree[--A[i]].push_back(--B[i]);
        Tree[B[i]].push_back(A[i]);
    }

    vector<bool> isPath(N);
    vector<int> path;
    auto dfs = [&](auto &&self, const int v, const int p) -> bool {
        bool ret = false;
        for (const int t : Tree[v]) {
            if (t == p) continue;
            ret |= self(self, t, v);
        }
        if (v == T) ret = true;
        isPath[v] = ret;
        if (ret) path.push_back(v);
        return ret;
    };
    dfs(dfs, M, -1);

    vector<i64> dp(N);
    auto dfs2 = [&](auto &&self, const int v, const int p) -> i64 {
        vector<int> vals;
        for (const int t : Tree[v]) {
            if (t == p) continue;
            if (isPath[t]) continue;
            vals.push_back(self(self, t, v));
        }
        sort(ALL(vals), greater());
        i64 cost = -1;
        if (vals.empty()) cost = 0;
        else if (vals.size() == 1) cost = 1;
        else cost = vals[1] + (int)(vals.size()) - 1;
        dp[v] = cost;
        return dp[v] + 1;
    };

    i64 ans = dfs2(dfs2, M, -1) - 1;
    for (const auto e : path) {
        if (e == M or e == T) continue;
        ans += (int)Tree[e].size() - 2;
    }

    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    main_();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 70796 KB Output is correct
2 Correct 249 ms 63748 KB Output is correct
3 Correct 777 ms 72128 KB Output is correct
4 Correct 306 ms 36432 KB Output is correct
5 Correct 747 ms 71996 KB Output is correct
6 Correct 732 ms 72124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Incorrect 1 ms 604 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 241 ms 70796 KB Output is correct
12 Correct 249 ms 63748 KB Output is correct
13 Correct 777 ms 72128 KB Output is correct
14 Correct 306 ms 36432 KB Output is correct
15 Correct 747 ms 71996 KB Output is correct
16 Correct 732 ms 72124 KB Output is correct
17 Incorrect 1 ms 604 KB Output isn't correct
18 Halted 0 ms 0 KB -