답안 #574255

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
574255 2022-06-08T08:58:22 Z tengiz05 Village (BOI20_village) C++17
75 / 100
106 ms 25928 KB
#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    int mn = 0;
    
    std::vector<int> w(n, -1), o(n, -1);
    std::vector<int> siz(n, 1);
    
    std::function<void(int, int)> dfs = [&](int u, int p) {
        std::vector<int> a;
        int x = -1;
        for (int v : adj[u]) {
            if (v == p)
                continue;
            dfs(v, u);
            siz[u] += siz[v];
            if (w[v] == -1) {
                a.push_back(v);
            }
            x = v;
        }
        int m = a.size();
        if (m % 2 == 1) {
            for (int i = 0; i < m - 2; i += 2) {
                mn += 4;
                w[a[i]] = a[i + 1];
                w[a[i + 1]] = a[i];
            }
            mn += 2;
            w[u] = a[m - 1];
            w[a[m - 1]] = u;
        } else {
            for (int i = 0; i < m; i += 2) {
                mn += 4;
                w[a[i]] = a[i + 1];
                w[a[i + 1]] = a[i];
            }
            if (m >= 2) {
                w[u] = a[0];
                w[a[0]] = a[1];
                w[a[1]] = u;
            } else if (p == -1) {
                mn += 2;
                w[u] = w[x];
                w[x] = u;
            }
        }
    };
    
    dfs(0, -1);
    
    std::function<int(int, int)> dfs2 = [&](int u, int p) {
        for (int v : adj[u]) {
            if (v != p && siz[v] > n / 2) {
                return dfs2(v, u);
            }
        }
        return u;
    };
    
    int u = dfs2(0, -1);
    
    int mx = 0;
    
    std::vector<std::vector<int>> a(n);
    
    std::priority_queue<std::pair<int, int>> q;
    
    for (int v : adj[u]) {
        std::vector<int> b;
        std::function<void(int, int, int)> dfs = [&](int u, int p, int dep) {
            b.push_back(u);
            mx += dep;
            for (int v : adj[u]) {
                if (v != p) {
                    dfs(v, u, dep + 1);
                }
            }
        };
        dfs(v, u, 1);
        a[v] = b;
        q.emplace(b.size(), v);
    }
    
    while (q.size() > 1) {
        auto [si, i] = q.top();
        q.pop();
        auto [sj, j] = q.top();
        q.pop();
        o[a[i].back()] = a[j].back();
        o[a[j].back()] = a[i].back();
        a[i].pop_back();
        a[j].pop_back();
        if (si > 1) {
            q.emplace(si - 1, i);
        }
        if (sj > 1) {
            q.emplace(sj - 1, j);
        }
    }
    
    if (q.empty()) {
        int v = adj[u][0];
        o[u] = o[v];
        o[v] = u;
    } else {
        auto [si, i] = q.top();
        assert(a[i].size() == 1);
        o[u] = a[i][0];
        o[a[i][0]] = u;
    }
    
    std::cout << mn << " " << 2 * mx << "\n";
    
    for (int i = 0; i < n; i++) {
        std::cout << w[i] + 1 << " \n"[i == n - 1];
    }
    
    for (int i = 0; i < n; i++) {
        std::cout << o[i] + 1 << " \n"[i == n - 1];
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 324 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 452 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 448 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 324 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 452 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 448 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 316 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 88 ms 11152 KB Output is correct
49 Correct 80 ms 12292 KB Output is correct
50 Correct 71 ms 12268 KB Output is correct
51 Correct 49 ms 9572 KB Output is correct
52 Correct 71 ms 12052 KB Output is correct
53 Correct 76 ms 10940 KB Output is correct
54 Correct 36 ms 12516 KB Output is correct
55 Partially correct 99 ms 25928 KB Partially correct
56 Partially correct 89 ms 18788 KB Partially correct
57 Correct 106 ms 16808 KB Output is correct
58 Correct 81 ms 14644 KB Output is correct
59 Correct 69 ms 12564 KB Output is correct
60 Correct 100 ms 15836 KB Output is correct
61 Correct 78 ms 13180 KB Output is correct
62 Correct 67 ms 12756 KB Output is correct
63 Correct 90 ms 12112 KB Output is correct
64 Correct 65 ms 12488 KB Output is correct
65 Correct 64 ms 12680 KB Output is correct
66 Correct 62 ms 11928 KB Output is correct
67 Correct 44 ms 9840 KB Output is correct
68 Correct 60 ms 10804 KB Output is correct
69 Correct 65 ms 12660 KB Output is correct
70 Correct 83 ms 11904 KB Output is correct
71 Correct 41 ms 9060 KB Output is correct
72 Correct 45 ms 10036 KB Output is correct
73 Correct 71 ms 12728 KB Output is correct
74 Correct 74 ms 11736 KB Output is correct
75 Correct 94 ms 12196 KB Output is correct
76 Correct 65 ms 12060 KB Output is correct
77 Correct 68 ms 11956 KB Output is correct
78 Correct 41 ms 8260 KB Output is correct
79 Correct 46 ms 9612 KB Output is correct
80 Correct 77 ms 12228 KB Output is correct
81 Correct 70 ms 12044 KB Output is correct
82 Correct 70 ms 12328 KB Output is correct