Submission #869683

#TimeUsernameProblemLanguageResultExecution timeMemory
869683wakandaaaVillage (BOI20_village)C++17
100 / 100
69 ms47548 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;

#define file ""

#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()

#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1LL << (x))
#define popcount __builtin_popcountll

mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    return l + rd() % (r - l + 1);
}

const int N = 1e6 + 5;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;

template<class X, class Y> bool mini(X &a, Y b) {
    return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
    return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

int n;
vector<int> adj[N];

namespace task_min {

int par[N];
int ans[N];
int res = 0;

void dfs(int u, int p) {
    for (int v : adj[u]) if (v != p) {
        dfs(v, u);
        if (!ans[u] && !ans[v]) {
            ans[u] = v;
            ans[v] = u;
            res += 2;
        }
    }
}

pair<int, vector<int> > solve() {
    dfs(1, 0);

    for (int i = 1; i <= n; ++i) if (ans[i] == 0) {
        int u = adj[i][0];
        ans[i] = ans[u];
        ans[u] = i;
        res += 2;
    }

    vector<int> pos;
    for (int i = 1; i <= n; ++i) pos.push_back(ans[i]);
    return mp(res, pos);
}

}

namespace task_max {

vector<int> topo;
int sz[N];
int fin[N];
int res = 0;

void dfs(int u, int p) {
    fin[u] = (int) topo.size();
    topo.push_back(u);
    sz[u] = 1;
    for (int v : adj[u]) if (v != p) {
        dfs(v, u);
        sz[u] += sz[v];
    }
    res += 2 * min(sz[u], n - sz[u]);
}

pair<int, vector<int> > solve() {
    dfs(1, 0);

    vector<int> pos;
    for (int i = 1; i <= n; ++i) pos.push_back(topo[(fin[i] + n / 2) % n]);
    return mp(res, pos);
}

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);

    cin >> n;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    pair<int, vector<int> > mn = task_min::solve(), mx = task_max::solve();

    cout << mn.fi << ' ' << mx.fi << '\n';
    for (int i : mn.se) cout << i << ' ';
    cout << '\n';
    for (int i : mx.se) cout << i << ' ';

    return 0;
}

/*

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...