답안 #575709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
575709 2022-06-11T08:21:50 Z talant117408 Village (BOI20_village) C++17
75 / 100
134 ms 22492 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'

void solve() {
    int n;
    cin >> n;
    vector <int> graph[n];
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    
    int cost[2];
    cost[0] = cost[1] = 0;
    vector <int> subtree(n);
    vector <vector <int>> rear(2, vector <int> (n, 0));
    
    function <int(int, int)> find_smallest = [&](int v, int p) {
        vector <int> children;
        subtree[v] = 1;
        for (auto to : graph[v]) {
            if (to == p) continue;
            if (find_smallest(to, v)) {
                children.pb(to);
            }
            subtree[v] += subtree[to];
        }
        if (sz(children) == 0) {
            if (v) return 1;
            cost[0] += 2;
            int x = graph[v][0];
            rear[0][v] = rear[0][x];
            rear[0][x] = v;
        }
        else if (sz(children)&1) {
            cost[0] += int(sz(children)/2)*4+2;
            rear[0][v] = children[0];
            rear[0][children[0]] = v;
            for (int i = 1; i < sz(children); i += 2) {
                rear[0][children[i]] = children[i+1];
                rear[0][children[i+1]] = children[i];
            }
        }
        else {
            cost[0] += sz(children)*2;
            rear[0][v] = children[1];
            rear[0][children[1]] = children[0];
            rear[0][children[0]] = v;
            for (int i = 2; i < sz(children); i += 2) {
                rear[0][children[i]] = children[i+1];
                rear[0][children[i+1]] = children[i];
            }
        }
        return 0;
    };
    find_smallest(0, 0);
    
    function <int(int, int)> find_centroid = [&](int v, int p) {
        for (auto to : graph[v]) {
            if (to == p) continue;
            if (subtree[to]*2 > subtree[0]) {
                return find_centroid(to, v);
            }
        }
        return v;
    };
    function <void(int, int)> find_largest = [&](int v, int p) {
        if (v) {
            cost[1] += min(subtree[v], subtree[0]-subtree[v])*2;
        }
        for (auto to : graph[v]) {
            if (to == p) continue;
            find_largest(to, v);
        }
    };
    int centroid = find_centroid(0, 0);
    find_largest(0, 0);
    vector <vector <int>> children(sz(graph[centroid]));
    function <void(int, int, int)> find_children = [&](int v, int p, int i) {
        children[i].pb(v);
        for (auto to : graph[v]) {
            if (to == p) continue;
            find_children(to, v, i);
        }
    };
    for (int i = 0; i < sz(graph[centroid]); i++) {
        find_children(graph[centroid][i], centroid, i);
    }
    //~ if (n&1) {
        //~ rear[1][centroid] = children[1].back();
        //~ rear[1][children[1].back()] = children[0].back();
        //~ rear[1][children[0].back()] = centroid;
        //~ children[0].pop_back();
        //~ children[1].pop_back();
    //~ }
    //~ else {
        //~ rear[1][centroid] = children[0].back();
        //~ rear[1][children[0].back()] = centroid;
        //~ children[0].pop_back();
    //~ }
    set <pii, greater <pii>> st;
    for (int i = 0; i < sz(children); i++) {
        if (sz(children[i])) {
            st.insert(mp(sz(children[i]), i));
        }
    }
    if (n&1) {
        auto l = *st.begin(); st.erase(st.begin());
        auto r = *st.begin(); st.erase(st.begin());
        rear[1][centroid] = children[r.second].back();
        rear[1][children[r.second].back()] = children[l.second].back();
        rear[1][children[l.second].back()] = centroid;
        children[l.second].pop_back();
        children[r.second].pop_back();
        l.first--; r.first--;
        if (l.first) st.insert(l);
        if (r.first) st.insert(r);
    }
    else {
        auto l = *st.begin(); st.erase(st.begin());
        rear[1][centroid] = children[l.second].back();
        rear[1][children[l.second].back()] = centroid;
        children[l.second].pop_back();
        l.first--;
        if (l.first) st.insert(l);
    }
    while (sz(st)) {
        auto l = *st.begin(); st.erase(st.begin());
        auto r = *st.begin(); st.erase(st.begin());
        l.first--; r.first--;
        rear[1][children[l.second].back()] = children[r.second].back();
        rear[1][children[r.second].back()] = children[l.second].back();
        children[l.second].pop_back();
        children[r.second].pop_back();
        if (l.first) st.insert(l);
        if (r.first) st.insert(r);
    }
    
    cout << cost[0] << ' ' << cost[1] << endl;
    for (int i = 0; i < 2; i++) {
        for (auto to : rear[i]) cout << to+1 << ' ';
        cout << endl;
    }
}

int main() {
    //~ do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}
/*
7
4 2
5 7
3 4
6 3
1 3
4 5
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 308 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 296 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 304 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 428 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 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 308 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 1 ms 304 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 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 308 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 296 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 304 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 428 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 1 ms 304 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 1 ms 308 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 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 308 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 296 KB Output is correct
45 Correct 1 ms 304 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 104 ms 8356 KB Output is correct
49 Correct 126 ms 8944 KB Output is correct
50 Correct 110 ms 9064 KB Output is correct
51 Correct 83 ms 7028 KB Output is correct
52 Correct 108 ms 8776 KB Output is correct
53 Correct 98 ms 8136 KB Output is correct
54 Correct 55 ms 10872 KB Output is correct
55 Partially correct 134 ms 22492 KB Partially correct
56 Partially correct 127 ms 15444 KB Partially correct
57 Correct 118 ms 13256 KB Output is correct
58 Correct 115 ms 11236 KB Output is correct
59 Correct 108 ms 9156 KB Output is correct
60 Correct 111 ms 18792 KB Output is correct
61 Correct 100 ms 10956 KB Output is correct
62 Correct 97 ms 9336 KB Output is correct
63 Correct 87 ms 8984 KB Output is correct
64 Correct 101 ms 9220 KB Output is correct
65 Correct 113 ms 9528 KB Output is correct
66 Correct 93 ms 8916 KB Output is correct
67 Correct 74 ms 8044 KB Output is correct
68 Correct 85 ms 7936 KB Output is correct
69 Correct 96 ms 9360 KB Output is correct
70 Correct 89 ms 8856 KB Output is correct
71 Correct 64 ms 6584 KB Output is correct
72 Correct 72 ms 7456 KB Output is correct
73 Correct 94 ms 9564 KB Output is correct
74 Correct 88 ms 8592 KB Output is correct
75 Correct 111 ms 8908 KB Output is correct
76 Correct 124 ms 8768 KB Output is correct
77 Correct 95 ms 8924 KB Output is correct
78 Correct 64 ms 6124 KB Output is correct
79 Correct 82 ms 7152 KB Output is correct
80 Correct 131 ms 8736 KB Output is correct
81 Correct 109 ms 8988 KB Output is correct
82 Correct 94 ms 9024 KB Output is correct