Submission #693205

# Submission time Handle Problem Language Result Execution time Memory
693205 2023-02-02T13:07:12 Z leeh18 Race (IOI11_race) C++17
100 / 100
605 ms 30628 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

int best_path(int N, int K, int H[][2], int L[]) {
    vector<vector<pair<int, int>>> adj(N);
    for (auto i = 0; i < N - 1; ++i) {
        auto u = H[i][0];
        auto v = H[i][1];
        adj[u].emplace_back(v, L[i]);
        adj[v].emplace_back(u, L[i]);
    }
    constexpr auto inf = numeric_limits<int>::max() / 2;
    vector<int> subtree_size(N), r(K + 1, inf);
    vector<bool> done(N);
    r[0] = 0;
    auto ans = inf;
    auto dfs_size = [&](auto self, int u, int p) -> int {
        auto &ret = subtree_size[u];
        ret = 1;
        for (auto [v, w] : adj[u]) {
            if (v != p && !done[v]) {
                ret += self(self, v, u);
            }
        }
        return ret;
    };
    auto dfs_centroid = [&](auto self, int u, int p, int sz) -> int {
        for (auto [v, w] : adj[u]) {
            if (v != p && !done[v] && sz / 2 < subtree_size[v]) {
                return self(self, v, u, sz);
            }
        }
        return u;
    };
    auto dfs = [&](auto self, int u, int p, int d, int cnt, auto f) -> void {
        if (K < d) {
            return;
        }
        f(d, cnt);
        for (auto [v, w] : adj[u]) {
            if (v != p && !done[v]) {
                self(self, v, u, d + w, cnt + 1, f);
            }
        }
    };
    auto centroid_decomposition = [&](auto self, int u) -> void {
        auto sz = dfs_size(dfs_size, u, u);
        u = dfs_centroid(dfs_centroid, u, u, sz);
        done[u] = true;
        for (auto [v, w] : adj[u]) {
            if (!done[v]) {
                dfs(dfs, v, u, w, 1,
                    [&](int d, int cnt) { ans = min(ans, r[K - d] + cnt); });
                dfs(dfs, v, u, w, 1,
                    [&](int d, int cnt) { r[d] = min(r[d], cnt); });
            }
        }
        for (auto [v, w] : adj[u]) {
            if (!done[v]) {
                dfs(dfs, v, u, w, 1,
                    [&](int d, [[maybe_unused]] int cnt) { r[d] = inf; });
            }
        }
        for (auto [v, w] : adj[u]) {
            if (!done[v]) {
                self(self, v);
            }
        }
    };
    centroid_decomposition(centroid_decomposition, 0);
    if (ans == inf) {
        ans = -1;
    }
    return ans;
}
# Verdict Execution time Memory 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 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory 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 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 4 ms 3924 KB Output is correct
23 Correct 3 ms 3328 KB Output is correct
24 Correct 3 ms 3668 KB Output is correct
25 Correct 4 ms 3668 KB Output is correct
26 Correct 2 ms 1620 KB Output is correct
27 Correct 4 ms 3412 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 3 ms 1620 KB Output is correct
30 Correct 2 ms 1748 KB Output is correct
31 Correct 3 ms 2900 KB Output is correct
32 Correct 3 ms 3156 KB Output is correct
33 Correct 4 ms 3412 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 3 ms 3540 KB Output is correct
36 Correct 4 ms 3924 KB Output is correct
37 Correct 4 ms 3412 KB Output is correct
38 Correct 3 ms 2388 KB Output is correct
# Verdict Execution time Memory 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 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 156 ms 7844 KB Output is correct
20 Correct 136 ms 7772 KB Output is correct
21 Correct 190 ms 7916 KB Output is correct
22 Correct 132 ms 7980 KB Output is correct
23 Correct 79 ms 8152 KB Output is correct
24 Correct 68 ms 7912 KB Output is correct
25 Correct 158 ms 9560 KB Output is correct
26 Correct 125 ms 12056 KB Output is correct
27 Correct 169 ms 15564 KB Output is correct
28 Correct 363 ms 22732 KB Output is correct
29 Correct 247 ms 22128 KB Output is correct
30 Correct 206 ms 15664 KB Output is correct
31 Correct 214 ms 15716 KB Output is correct
32 Correct 213 ms 15652 KB Output is correct
33 Correct 243 ms 14484 KB Output is correct
34 Correct 170 ms 14420 KB Output is correct
# Verdict Execution time Memory 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 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 4 ms 3924 KB Output is correct
23 Correct 3 ms 3328 KB Output is correct
24 Correct 3 ms 3668 KB Output is correct
25 Correct 4 ms 3668 KB Output is correct
26 Correct 2 ms 1620 KB Output is correct
27 Correct 4 ms 3412 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 3 ms 1620 KB Output is correct
30 Correct 2 ms 1748 KB Output is correct
31 Correct 3 ms 2900 KB Output is correct
32 Correct 3 ms 3156 KB Output is correct
33 Correct 4 ms 3412 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 3 ms 3540 KB Output is correct
36 Correct 4 ms 3924 KB Output is correct
37 Correct 4 ms 3412 KB Output is correct
38 Correct 3 ms 2388 KB Output is correct
39 Correct 156 ms 7844 KB Output is correct
40 Correct 136 ms 7772 KB Output is correct
41 Correct 190 ms 7916 KB Output is correct
42 Correct 132 ms 7980 KB Output is correct
43 Correct 79 ms 8152 KB Output is correct
44 Correct 68 ms 7912 KB Output is correct
45 Correct 158 ms 9560 KB Output is correct
46 Correct 125 ms 12056 KB Output is correct
47 Correct 169 ms 15564 KB Output is correct
48 Correct 363 ms 22732 KB Output is correct
49 Correct 247 ms 22128 KB Output is correct
50 Correct 206 ms 15664 KB Output is correct
51 Correct 214 ms 15716 KB Output is correct
52 Correct 213 ms 15652 KB Output is correct
53 Correct 243 ms 14484 KB Output is correct
54 Correct 170 ms 14420 KB Output is correct
55 Correct 11 ms 1108 KB Output is correct
56 Correct 11 ms 1048 KB Output is correct
57 Correct 93 ms 8104 KB Output is correct
58 Correct 35 ms 8136 KB Output is correct
59 Correct 127 ms 14132 KB Output is correct
60 Correct 522 ms 30628 KB Output is correct
61 Correct 197 ms 18780 KB Output is correct
62 Correct 207 ms 22584 KB Output is correct
63 Correct 315 ms 22492 KB Output is correct
64 Correct 605 ms 20952 KB Output is correct
65 Correct 250 ms 19516 KB Output is correct
66 Correct 409 ms 27996 KB Output is correct
67 Correct 112 ms 23264 KB Output is correct
68 Correct 275 ms 23296 KB Output is correct
69 Correct 308 ms 23468 KB Output is correct
70 Correct 235 ms 22440 KB Output is correct