Submission #395376

# Submission time Handle Problem Language Result Execution time Memory
395376 2021-04-28T09:51:24 Z KoD Village (BOI20_village) C++17
75 / 100
170 ms 22720 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, 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) {
                    (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) {
                    (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;
}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:118:32: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  118 |                     (up.size() < N / 2 ? up : down).push_back(u);
      |                      ~~~~~~~~~~^~~~~~~
Village.cpp:130:32: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  130 |                     (up.size() < (N - 1) / 2 ? up : down).push_back(u);
      |                      ~~~~~~~~~~^~~~~~~~~~~~~
# 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 292 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 208 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 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 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 460 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 432 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 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 2 ms 304 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 428 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 428 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 292 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 208 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 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 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 460 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 432 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 332 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 2 ms 336 KB Output is correct
41 Correct 2 ms 304 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 428 KB Output is correct
46 Correct 2 ms 332 KB Output is correct
47 Correct 2 ms 428 KB Output is correct
48 Correct 133 ms 12592 KB Output is correct
49 Correct 154 ms 13764 KB Output is correct
50 Correct 159 ms 13764 KB Output is correct
51 Correct 109 ms 10784 KB Output is correct
52 Correct 145 ms 13576 KB Output is correct
53 Correct 130 ms 12308 KB Output is correct
54 Correct 74 ms 11132 KB Output is correct
55 Partially correct 170 ms 22720 KB Partially correct
56 Partially correct 164 ms 17860 KB Partially correct
57 Correct 168 ms 16316 KB Output is correct
58 Correct 159 ms 15164 KB Output is correct
59 Correct 151 ms 13888 KB Output is correct
60 Correct 134 ms 15544 KB Output is correct
61 Correct 128 ms 15032 KB Output is correct
62 Correct 133 ms 14984 KB Output is correct
63 Correct 128 ms 14040 KB Output is correct
64 Correct 142 ms 14668 KB Output is correct
65 Correct 137 ms 14964 KB Output is correct
66 Correct 126 ms 14136 KB Output is correct
67 Correct 94 ms 11236 KB Output is correct
68 Correct 112 ms 12732 KB Output is correct
69 Correct 133 ms 15036 KB Output is correct
70 Correct 123 ms 14124 KB Output is correct
71 Correct 89 ms 10472 KB Output is correct
72 Correct 104 ms 11964 KB Output is correct
73 Correct 131 ms 15112 KB Output is correct
74 Correct 122 ms 13820 KB Output is correct
75 Correct 150 ms 13696 KB Output is correct
76 Correct 144 ms 13736 KB Output is correct
77 Correct 135 ms 14000 KB Output is correct
78 Correct 96 ms 9660 KB Output is correct
79 Correct 106 ms 11168 KB Output is correct
80 Correct 150 ms 13684 KB Output is correct
81 Correct 134 ms 14324 KB Output is correct
82 Correct 134 ms 14656 KB Output is correct