Submission #254811

# Submission time Handle Problem Language Result Execution time Memory
254811 2020-07-30T15:59:43 Z imeimi2000 Village (BOI20_village) C++17
100 / 100
143 ms 21616 KB
#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> edge[100001];

namespace solve1 {
    int dp[100001][2], pr[100001];
    void dfs(int x, int p) {
        dp[x][0] = 0;
        dp[x][1] = 1;
        int mn = 1e8;
        for (int i : edge[x]) {
            if (i == p) continue;
            dfs(i, x);
            int val = min(dp[i][0], dp[i][1]);
            if (dp[i][1] - val < mn) mn = dp[i][1] - val, pr[x] = i;
            dp[x][0] += val;
            dp[x][1] += val;
        }
        dp[x][0] += mn;
    }

    vector<int> lst[100001];
    int g[100001];
    void dfs2(int x, int p, int j) {
        if (j) lst[g[x] = g[p]].push_back(x);
        else lst[g[x] = x].push_back(x);
        for (int i : edge[x]) {
            if (i == p) continue;
            if (j) {
                dfs2(i, x, dp[i][0] > dp[i][1]);
            }
            else {
                dfs2(i, x, i == pr[x] || dp[i][0] > dp[i][1]);
            }
        }
    }
    int ans[100001];
    long long solve() {
        dfs(1, 0);
        dfs2(1, 0, 0);
        for (int i = 1; i <= n; ++i) {
            int sz = lst[i].size();
            if (!sz) continue;
            for (int j = 0; j < sz; ++j) {
                ans[lst[i][j]] = lst[i][(j + 1) % sz];
            }
        }
        return dp[1][0] * 2;
    }
    void print() {
        for (int i = 1; i <= n; ++i) {
            printf("%d%c", ans[i], "\n "[i < n]);
        }
    }
}

namespace solve2 {
    int sz[100001];

    void dfs(int x, int p) {
        sz[x] = 1;
        for (int i : edge[x]) {
            if (i == p) continue;
            dfs(i, x);
            sz[x] += sz[i];
        }
    }

    vector<int> lst;
    long long sum = 0;
    void dfs2(int x, int p, int d = 0) {
        lst.push_back(x);
        sum += d;
        for (int i : edge[x]) {
            if (i == p) continue;
            dfs2(i, x, d + 1);
        }
    }

    int ans[100001];
    long long solve() {
        dfs(1, 0);
        int x = 1;
        for (int p = 0, loop = 1; loop; ) {
            loop = 0;
            for (int i : edge[x]) {
                if (i == p) continue;
                if (sz[i] * 2 > n) {
                    p = x;
                    x = i;
                    loop = 1;
                    break;
                }
            }
        }
        dfs2(x, 0);
        for (int i = 0; i < n; ++i) {
            ans[lst[i]] = lst[(i + n / 2) % n];
        }
        return sum * 2;
    }
    void print() {
        for (int i = 1; i <= n; ++i) {
            printf("%d%c", ans[i], "\n "[i < n]);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    printf("%lld %lld\n", solve1::solve(), solve2::solve());
    solve1::print();
    solve2::print();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 3 ms 5120 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 3 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 5 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5248 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 5 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 3 ms 5120 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 5 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5120 KB Output is correct
19 Correct 3 ms 5120 KB Output is correct
20 Correct 4 ms 5120 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 4 ms 5120 KB Output is correct
24 Correct 4 ms 5120 KB Output is correct
25 Correct 4 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 6 ms 5120 KB Output is correct
28 Correct 4 ms 5120 KB Output is correct
29 Correct 4 ms 5120 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 3 ms 5120 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 4 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 3 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 5 ms 5120 KB Output is correct
18 Correct 3 ms 5120 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5120 KB Output is correct
21 Correct 4 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 4 ms 5120 KB Output is correct
24 Correct 4 ms 5248 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 4 ms 5120 KB Output is correct
27 Correct 4 ms 5120 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 4 ms 5120 KB Output is correct
30 Correct 4 ms 5120 KB Output is correct
31 Correct 3 ms 5120 KB Output is correct
32 Correct 4 ms 5120 KB Output is correct
33 Correct 5 ms 5120 KB Output is correct
34 Correct 4 ms 5120 KB Output is correct
35 Correct 4 ms 5120 KB Output is correct
36 Correct 3 ms 5120 KB Output is correct
37 Correct 4 ms 5120 KB Output is correct
38 Correct 4 ms 5120 KB Output is correct
39 Correct 4 ms 5120 KB Output is correct
40 Correct 4 ms 5120 KB Output is correct
41 Correct 4 ms 5120 KB Output is correct
42 Correct 4 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 4 ms 5120 KB Output is correct
46 Correct 4 ms 5120 KB Output is correct
47 Correct 4 ms 5120 KB Output is correct
48 Correct 110 ms 13040 KB Output is correct
49 Correct 130 ms 13812 KB Output is correct
50 Correct 107 ms 13816 KB Output is correct
51 Correct 75 ms 11888 KB Output is correct
52 Correct 133 ms 13808 KB Output is correct
53 Correct 110 ms 12788 KB Output is correct
54 Correct 55 ms 13176 KB Output is correct
55 Correct 143 ms 21616 KB Output is correct
56 Correct 142 ms 17516 KB Output is correct
57 Correct 124 ms 16244 KB Output is correct
58 Correct 125 ms 15220 KB Output is correct
59 Correct 103 ms 13804 KB Output is correct
60 Correct 62 ms 13040 KB Output is correct
61 Correct 69 ms 13168 KB Output is correct
62 Correct 88 ms 13424 KB Output is correct
63 Correct 81 ms 12920 KB Output is correct
64 Correct 100 ms 13688 KB Output is correct
65 Correct 80 ms 13684 KB Output is correct
66 Correct 70 ms 13044 KB Output is correct
67 Correct 89 ms 11120 KB Output is correct
68 Correct 67 ms 12400 KB Output is correct
69 Correct 89 ms 13676 KB Output is correct
70 Correct 88 ms 13168 KB Output is correct
71 Correct 57 ms 10992 KB Output is correct
72 Correct 62 ms 11888 KB Output is correct
73 Correct 76 ms 13680 KB Output is correct
74 Correct 70 ms 12912 KB Output is correct
75 Correct 113 ms 13548 KB Output is correct
76 Correct 131 ms 13424 KB Output is correct
77 Correct 91 ms 13428 KB Output is correct
78 Correct 68 ms 10608 KB Output is correct
79 Correct 79 ms 11500 KB Output is correct
80 Correct 108 ms 13552 KB Output is correct
81 Correct 111 ms 13540 KB Output is correct
82 Correct 99 ms 13300 KB Output is correct