Submission #575644

# Submission time Handle Problem Language Result Execution time Memory
575644 2022-06-11T07:08:55 Z talant117408 Village (BOI20_village) C++17
0 / 100
1 ms 212 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, -1));
    
    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();
    }
    int l = 0, r = 1;
    while (1) {
        while (l < sz(children) && sz(children[l]) == 0) l++;
        while (r < sz(children) && sz(children[r]) == 0) r++;
        if (r == l) r++;
        while (r < sz(children) && sz(children[r]) == 0) r++;
        
        if (l >= sz(children) || r >= sz(children)) {
            break;
        }
        
        rear[1][children[l].back()] = children[r].back();
        rear[1][children[r].back()] = children[l].back();
        children[l].pop_back();
        children[r].pop_back();
    }
    
    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
*/
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=vi] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Integer parameter [name=vi] equals to 0, violates the range [1, 256]
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Integer parameter [name=vi] equals to 0, violates the range [1, 4]
2 Halted 0 ms 0 KB -