#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int S[200005], cnt = 1, E[200005], p[20][200005];
vector <int> adj[200005];
int n, dep[200005];
void dfs(int x, int par, int d){
S[x] = cnt++;
p[0][x] = par;
dep[x] = d;
for(auto i : adj[x]){
if(i == par)continue;
dfs(i, x, d + 1);
}
}
int lca(int u, int v){
if(dep[u] > dep[v])swap(u, v);
int diff = dep[v] - dep[u];
for(int i=19;i>=0;i--)if(diff >> i & 1)v = p[i][v];
if(u == v)return u;
for(int i=19;i>=0;i--){
if(p[i][u] != p[i][v])u = p[i][u], v = p[i][v];
}
return p[0][u];
}
int hv[200005], ans, bru[200005], f = 0;
void grr(int x, int par){
vector <int> tmp;
for(auto i : adj[x]){
if(i == par)continue;
grr(i, x);
if(hv[i])tmp.push_back(i);
}
if((int)tmp.size() >= 2 && !f && n%2 && (int)tmp.size()%2 == 0){
int xx = tmp.back(); tmp.pop_back();
int y = tmp.back(); tmp.pop_back();
ans += dep[xx] + dep[y] - 2 * dep[lca(xx, y)];
bru[x] = xx;
bru[xx] = y;
bru[y] = x;
f = 1;
}
while((int)tmp.size() >= 2){
int xx = tmp.back(); tmp.pop_back();
int y = tmp.back(); tmp.pop_back();
ans += dep[xx] + dep[y] - 2 * dep[lca(xx, y)];
bru[xx] = y, bru[y] = xx;
}
if(tmp.empty())hv[x] = 1;
else ans += dep[tmp[0]] + dep[x] - 2 * dep[lca(tmp[0], x)], bru[x] = tmp[0], bru[tmp[0]] = x;
}
int B[500005];
void solve(){
cin >> n;
for(int i=1;i<n;i++){
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1, 1);
for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)p[i][j] = p[i-1][p[i-1][j]];
grr(1, -1);
if(!f && n % 2){
int x = adj[1][0];
int y = bru[x];
ans++;
bru[1] = x, bru[x] = y, bru[y] = 1;
}
cout << ans * 2 << ' ' << 0 << '\n';
for(int i=1;i<=n;i++)cout << bru[i] << ' ';
cout << '\n';
for(int i=1;i<=n;i++)cout << i << ' ';
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
Village.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
2 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
3 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
4 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
5 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
6 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
7 |
Partially correct |
3 ms |
5108 KB |
Partially correct |
8 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
9 |
Partially correct |
3 ms |
5160 KB |
Partially correct |
10 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
11 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
12 |
Partially correct |
3 ms |
5160 KB |
Partially correct |
13 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
2 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
3 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
4 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
5 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
6 |
Partially correct |
2 ms |
5076 KB |
Partially correct |
7 |
Partially correct |
3 ms |
5108 KB |
Partially correct |
8 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
9 |
Partially correct |
3 ms |
5160 KB |
Partially correct |
10 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
11 |
Partially correct |
3 ms |
5076 KB |
Partially correct |
12 |
Partially correct |
3 ms |
5160 KB |
Partially correct |
13 |
Incorrect |
2 ms |
5076 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |