#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()){
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 |
1 |
Correct |
4 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Runtime error |
9 ms |
9932 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
4 ms |
5068 KB |
Output is correct |
5 |
Correct |
6 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5088 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
5 ms |
5020 KB |
Output is correct |
9 |
Correct |
4 ms |
5028 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
5 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5028 KB |
Output is correct |
15 |
Correct |
4 ms |
5032 KB |
Output is correct |
16 |
Correct |
5 ms |
5028 KB |
Output is correct |
17 |
Correct |
4 ms |
5068 KB |
Output is correct |
18 |
Correct |
5 ms |
5068 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
4 ms |
5028 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5032 KB |
Output is correct |
24 |
Correct |
6 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
5 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5012 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5028 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Runtime error |
9 ms |
9932 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |