This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
const int MAXN = 1e5;
int n, s[MAXN], c, ans[2][MAXN], ansMax = 0, ansMin = 0;
vector<int> g[MAXN], a[MAXN];
void find(int u){
int mx = 0; s[u] = 1;
for(int v : g[u]){
if(s[v]) continue;
find(v), s[u] += s[v];
mx = max(mx, s[v]);
ansMax += min(n - s[v], s[v]);
if(ans[0][v] == v) swap(ans[0][u], ans[0][v]), ansMin += 2;
}
if(max(mx, n - s[u]) <= n/2) c = u;
}
void add(int u, int p, vector<int> &b){
b.push_back(u);
for(int v : g[u]) if(v != p) add(v, u, b);
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int x, y; cin >> n;
for(int i=1; i<n; ++i){
cin >> x >> y; --x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=0; i<2; ++i) iota(ans[i], ans[i]+n, 0LL);
find(0);
priority_queue<array<int, 2>> q;
if((x = n & 1)) swap(ans[1][c], ans[1][!c]);
for(int u : g[c]){
add(u, c, a[u]);
if(!x && (int)a[u].size() + 1 <= n/2) a[u].push_back(c), x = 1;
q.push({(int)a[u].size(), u});
}
while(!q.empty()){
assert(q.size() > 1);
array<int, 2> u = q.top(); q.pop();
array<int, 2> v = q.top(); q.pop();
swap(ans[1][a[u[1]].back()], ans[1][a[v[1]].back()]);
a[u[1]].pop_back(), a[v[1]].pop_back();
if(--u[0]) q.push(u);
if(--v[0]) q.push(v);
}
for(int i=0; i<n; ++i) if(ans[0][i] == i) swap(ans[0][i], ans[0][g[i][0]]), ansMin += 2;
cout << ansMin sp 2 * ansMax nl;
for(int i=0; i<2; ++i){
for(int j=0; j<n; ++j) cout << ans[i][j] + 1 << ' ';
cout nl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |