답안 #547935

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547935 2022-04-12T03:44:50 Z Jomnoi Village (BOI20_village) C++17
100 / 100
200 ms 25548 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int MAX_N = 1e5 + 10;

int timer;
long long ans1, ans2;
int sz[MAX_N], depth[MAX_N], parent[MAX_N][20];
vector <int> graph[MAX_N];
int pos[MAX_N], nhouse1[MAX_N], nhouse2[MAX_N];

void dfs1(const int &u, const int &p) {
    for(auto v : graph[u]) {
        if(v != p) {
            dfs1(v, u);
        }
    }

    if(nhouse1[u] == u) {
        ans1 += 2;
        if(p != -1) {
            swap(nhouse1[u], nhouse1[p]);
        }
        else {
            swap(nhouse1[1], nhouse1[graph[1][0]]);
        }
    }
}

int get_size(const int &u, const int &p) {
    sz[u] = 1;
    for(auto v : graph[u]) {
        if(v != p) {
            sz[u] += get_size(v, u);
        }
    }
    return sz[u];
}

int get_centroid(const int &u, const int &p, const int &n) {
    for(auto v : graph[u]) {
        if(v != p and sz[v] > n / 2) {
            return get_centroid(v, u, n);
        }
    }
    return u;
}

void dfs2(const int &u, const int &p) {
    pos[++timer] = u;
    for(int i = 1; i < 20; i++) {
        parent[u][i] = parent[parent[u][i - 1]][i - 1];
    }
    for(auto v : graph[u]) {
        if(v == p) {
            continue;
        }

        depth[v] = depth[u] + 1;
        parent[v][0] = u;
        dfs2(v, u);
    }
}

int find_lca(const int &a, const int &b) {
    int u = a, v = b;
    if(depth[u] < depth[v]) {
        swap(u, v);
    }
    for(int i = 19; i >= 0; i--) {
        if(depth[parent[u][i]] >= depth[v]) {
            u = parent[u][i];
        }
    }
    for(int i = 19; i >= 0; i--) {
        if(parent[u][i] != parent[v][i]) {
            u = parent[u][i];
            v = parent[v][i];
        }
    }
    return (u == v ? u : parent[u][0]);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int N;
    cin >> N;
    for(int i = 1; i <= N; i++) {
        nhouse1[i] = i;
    }
    for(int i = 1; i <= N - 1; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    dfs1(1, -1);
    dfs2(get_centroid(1, -1, get_size(1, -1)), -1);

    for(int i = 1; i <= N; i++) {
        nhouse2[pos[i]] = pos[(i + N / 2 - 1) % N + 1];
    }
    for(int i = 1; i <= N; i++) {
        ans2 += depth[i] + depth[nhouse2[i]] - 2 * depth[find_lca(i, nhouse2[i])];
    }

    cout << ans1 << ' ' << ans2 << '\n';
    for(int i = 1; i <= N; i++) {
        cout << nhouse1[i] << ' ';
    }
    cout << '\n';
    for(int i = 1; i <= N; i++) {
        cout << nhouse2[i] << ' ';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2684 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 3 ms 2772 KB Output is correct
6 Correct 3 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2816 KB Output is correct
12 Correct 3 ms 2816 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 3 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 3 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 3 ms 2772 KB Output is correct
21 Correct 2 ms 2816 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2816 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 3 ms 2680 KB Output is correct
28 Correct 3 ms 2772 KB Output is correct
29 Correct 3 ms 2812 KB Output is correct
30 Correct 3 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2684 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2684 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 3 ms 2772 KB Output is correct
27 Correct 3 ms 2772 KB Output is correct
28 Correct 4 ms 2816 KB Output is correct
29 Correct 3 ms 2816 KB Output is correct
30 Correct 3 ms 2772 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 3 ms 2772 KB Output is correct
38 Correct 2 ms 2816 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2772 KB Output is correct
41 Correct 2 ms 2816 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2772 KB Output is correct
44 Correct 3 ms 2680 KB Output is correct
45 Correct 3 ms 2772 KB Output is correct
46 Correct 3 ms 2812 KB Output is correct
47 Correct 3 ms 2772 KB Output is correct
48 Correct 105 ms 16572 KB Output is correct
49 Correct 139 ms 17964 KB Output is correct
50 Correct 128 ms 18024 KB Output is correct
51 Correct 113 ms 14616 KB Output is correct
52 Correct 120 ms 17876 KB Output is correct
53 Correct 132 ms 16304 KB Output is correct
54 Correct 65 ms 13728 KB Output is correct
55 Correct 200 ms 25548 KB Output is correct
56 Correct 171 ms 21428 KB Output is correct
57 Correct 187 ms 20156 KB Output is correct
58 Correct 184 ms 19096 KB Output is correct
59 Correct 155 ms 18056 KB Output is correct
60 Correct 88 ms 18112 KB Output is correct
61 Correct 93 ms 18380 KB Output is correct
62 Correct 146 ms 18412 KB Output is correct
63 Correct 92 ms 17460 KB Output is correct
64 Correct 109 ms 18252 KB Output is correct
65 Correct 120 ms 18432 KB Output is correct
66 Correct 99 ms 17532 KB Output is correct
67 Correct 70 ms 14248 KB Output is correct
68 Correct 105 ms 15948 KB Output is correct
69 Correct 97 ms 18380 KB Output is correct
70 Correct 89 ms 17480 KB Output is correct
71 Correct 79 ms 13664 KB Output is correct
72 Correct 75 ms 15060 KB Output is correct
73 Correct 118 ms 18440 KB Output is correct
74 Correct 103 ms 17100 KB Output is correct
75 Correct 162 ms 17876 KB Output is correct
76 Correct 127 ms 17896 KB Output is correct
77 Correct 122 ms 17528 KB Output is correct
78 Correct 65 ms 13000 KB Output is correct
79 Correct 123 ms 14584 KB Output is correct
80 Correct 136 ms 17816 KB Output is correct
81 Correct 144 ms 17612 KB Output is correct
82 Correct 105 ms 18188 KB Output is correct