Submission #395382

# Submission time Handle Problem Language Result Execution time Memory
395382 2021-04-28T09:55:43 Z KoD Village (BOI20_village) C++17
100 / 100
188 ms 21740 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

int main() {
    int N;
    std::cin >> N;
    Vec<Vec<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);
    }
    int min = -1;
    long long max = -1;
    Vec<int> min_v(N), max_v(N);
    // minimum
    {
        Vec<std::pair<int, int>> use;
        auto dfs = [&](auto&& dfs, const int u, const int p) -> bool {
            int child = -1;
            bool done = false;
            for (const int v: graph[u]) {
                if (v != p) {
                    if (dfs(dfs, v, u)) {
                        done = true;
                    }
                    child = v;
                }
            }
            if (!done) {
                if (p == -1) {
                    use.emplace_back(u, child);
                    return false;
                }
                use.emplace_back(u, p);
                return true;
            }
            return false;
        };
        dfs(dfs, 0, -1);
        min = 2 * (int) use.size();
        Vec<Vec<int>> tree(N);
        for (const auto [u, v]: use) {
            tree[u].push_back(v);
            tree[v].push_back(u);
        }
        Vec<bool> done(N);
        Vec<int> order;
        auto build = [&](auto&& build, const int u, const int p) -> void {
            done[u] = true;
            order.push_back(u);
            for (const auto v: tree[u]) {
                if (v != p) {
                    build(build, v, u);
                }
            }
        };
        for (int r = 0; r < N; ++r) {
            if (!done[r]) {
                build(build, r, -1);
                const auto len = (int) order.size();
                for (int i = 0; i < len; ++i) {
                    min_v[order[i]] = order[(i + 1) % len];
                }
                order.clear();
            }
        }
    }
    // maximum
    {
        max = 0;
        Vec<int> subtree(N);
        auto setup = [&](auto&& setup, const int u, const int p) -> void {
            subtree[u] = 1;
            for (const auto v: graph[u]) {
                if (v != p) {
                    setup(setup, v, u);
                    subtree[u] += subtree[v];
                }
            }
            if (p != -1) {
                max += 2 * std::min(subtree[u], N - subtree[u]);
            }
        };
        setup(setup, 0, -1);
        auto find = [&](auto&& find, const int u, const int p) -> int {
            for (const auto v: graph[u]) {
                if (v != p and subtree[v] * 2 > N) {
                    return find(find, v, u);
                }
            }
            return u;
        };
        const auto cent = find(find, 0, -1);
        Vec<Vec<int>> group;
        auto listup = [&](auto&& listup, const int u, const int p) -> void {
            group.back().push_back(u);
            for (const auto v: graph[u]) {
                if (v != p) {
                    listup(listup, v, u);
                }
            }
        };
        for (const auto u: graph[cent]) {
            group.push_back({});
            listup(listup, u, cent);
        }
        if (N % 2 == 0) {
            group.push_back({});
            group.back().push_back(cent);
            Vec<int> up, down;
            for (const auto& g: group) {
                for (const auto u: g) {
                    ((int) up.size() < N / 2 ? up : down).push_back(u);
                }
            }
            for (int i = 0; i < N / 2; ++i) {
                max_v[up[i]] = down[i];
                max_v[down[i]] = up[i];
            }
        }
        else {
            Vec<int> up, down;
            for (const auto& g: group) {
                for (const auto u: g) {
                    ((int) up.size() < (N - 1) / 2 ? up : down).push_back(u);
                }
            }
            for (int i = 0; i < (N - 1) / 2; ++i) {
                max_v[up[i]] = down[i];
                max_v[down[i]] = up[i];
            }            
            max_v[up[0]] = cent;
            max_v[cent] = down[0];
        }
    }
    std::cout << min << ' ' << max << '\n';
    for (int i = 0; i < N; ++i) {
        std::cout << min_v[i] + 1 << " \n"[i + 1 == N];
    }
    for (int i = 0; i < N; ++i) {
        std::cout << max_v[i] + 1 << " \n"[i + 1 == N];
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 428 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 428 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 428 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 2 ms 428 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 2 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 2 ms 332 KB Output is correct
48 Correct 139 ms 11712 KB Output is correct
49 Correct 152 ms 12924 KB Output is correct
50 Correct 160 ms 13028 KB Output is correct
51 Correct 110 ms 10172 KB Output is correct
52 Correct 157 ms 12808 KB Output is correct
53 Correct 131 ms 11548 KB Output is correct
54 Correct 77 ms 10820 KB Output is correct
55 Correct 188 ms 21740 KB Output is correct
56 Correct 164 ms 17032 KB Output is correct
57 Correct 159 ms 15424 KB Output is correct
58 Correct 172 ms 14288 KB Output is correct
59 Correct 157 ms 13116 KB Output is correct
60 Correct 136 ms 14740 KB Output is correct
61 Correct 134 ms 14268 KB Output is correct
62 Correct 143 ms 14148 KB Output is correct
63 Correct 127 ms 13232 KB Output is correct
64 Correct 153 ms 13880 KB Output is correct
65 Correct 142 ms 14124 KB Output is correct
66 Correct 125 ms 13368 KB Output is correct
67 Correct 94 ms 10616 KB Output is correct
68 Correct 113 ms 12024 KB Output is correct
69 Correct 136 ms 14184 KB Output is correct
70 Correct 128 ms 13368 KB Output is correct
71 Correct 93 ms 9928 KB Output is correct
72 Correct 106 ms 11320 KB Output is correct
73 Correct 133 ms 14276 KB Output is correct
74 Correct 129 ms 13036 KB Output is correct
75 Correct 162 ms 12816 KB Output is correct
76 Correct 153 ms 12788 KB Output is correct
77 Correct 139 ms 13240 KB Output is correct
78 Correct 92 ms 9044 KB Output is correct
79 Correct 109 ms 10432 KB Output is correct
80 Correct 161 ms 12704 KB Output is correct
81 Correct 146 ms 13492 KB Output is correct
82 Correct 141 ms 13676 KB Output is correct