Submission #533287

# Submission time Handle Problem Language Result Execution time Memory
533287 2022-03-05T08:49:09 Z KoD Mousetrap (CEOI17_mousetrap) C++17
100 / 100
874 ms 182156 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

template <class F> struct RecLambda : private F {
    explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> decltype(auto) operator() (Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, T, M;
    std::cin >> N >> T >> M;
    T -= 1, M -= 1;
    vector<vector<int>> graph(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b;
        std::cin >> a >> b;
        a -= 1, b -= 1;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<int> parent(N, -1);
    RecLambda([&](auto&& dfs, const int u) -> void {
        for (const int v : graph[u]) {
            if (parent[u] != v) {
                parent[v] = u;
                dfs(v);
            }
        }
    })(M);
    vector<int> path;
    for (int u = T; u != M;) {
        path.push_back(u = parent[u]);
    }
    const int L = path.size();
    vector<int> above(L);
    for (int i = 0; i < L - 1; ++i) {
        above[i + 1] = above[i] + (int)graph[path[i]].size() - 2;
    }
    vector<vector<int>> dp(L);
    for (int i = 0, last = T; i < L; ++i) {
        const int cur = path[i];
        for (const int c : graph[cur]) {
            if (c != parent[cur] and c != last) {
                dp[i].push_back(RecLambda([&](auto&& dfs, const int u, const int p) -> int {
                    vector<int> cand;
                    for (const int v : graph[u]) {
                        if (v != p) {
                            cand.push_back(dfs(v, u));
                        }
                    }
                    std::sort(cand.rbegin(), cand.rend());
                    const int size = (int)cand.size();
                    return size + (size <= 1 ? 0 : cand[1]);
                })(c, cur));
            }
        }
        last = cur;
    }
    const auto check = [&](const int thres) {
        int sofar = 0;
        for (int i = L - 1; i >= 0; --i) {
            int offset = sofar + (int)dp[i].size();
            for (const int c : dp[i]) {
                if (c + above[i] + offset > thres) {
                    if (sofar > (L - i - 1)) {
                        return false;
                    }
                    sofar += 1;
                }
            }
        }
        return sofar <= thres;
    };
    int ok = N, ng = -1;
    while (ok - ng > 1) {
        const int md = (ok + ng) / 2;
        (check(md) ? ok : ng) = md;
    }
    std::cout << ok << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 312 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 301 ms 59792 KB Output is correct
2 Correct 288 ms 65092 KB Output is correct
3 Correct 833 ms 73324 KB Output is correct
4 Correct 392 ms 36656 KB Output is correct
5 Correct 858 ms 73332 KB Output is correct
6 Correct 820 ms 73244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 312 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 312 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 301 ms 59792 KB Output is correct
12 Correct 288 ms 65092 KB Output is correct
13 Correct 833 ms 73324 KB Output is correct
14 Correct 392 ms 36656 KB Output is correct
15 Correct 858 ms 73332 KB Output is correct
16 Correct 820 ms 73244 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 326 ms 72356 KB Output is correct
31 Correct 313 ms 72316 KB Output is correct
32 Correct 346 ms 138320 KB Output is correct
33 Correct 389 ms 182156 KB Output is correct
34 Correct 874 ms 73376 KB Output is correct
35 Correct 842 ms 73268 KB Output is correct
36 Correct 865 ms 82744 KB Output is correct
37 Correct 847 ms 82688 KB Output is correct
38 Correct 663 ms 84556 KB Output is correct
39 Correct 618 ms 84608 KB Output is correct
40 Correct 648 ms 84760 KB Output is correct