// Alir3za.Zar3 -> Shiraz , Iran
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define loop(i , l , r) for (int i=l; i<=r; i+=1)
#define arc(i , r , l) for (int i=r; i>=l; i-=1)
const int N = 1e5+7;
int n,Lo,Hi=0 , lo[N],hi[N],sz[N],eu[N],q[N],T;
vector<int> e[N];
void brain_DFS (int v , int p)
{
sz[v] = 1;
eu[v]=++T; q[T]=v;
for (auto to : e[v])
{
if (to == p){ continue; }
brain_DFS(to,v);
sz[v] += sz[to];
Hi += min(sz[to],n-sz[to]);
}
if (lo[v] == v)
{
if (v==p)
swap(lo[v],lo[e[v].front()]);
else
swap(lo[v],lo[p]);
Lo += 2;
}
if (v!=1)
{
int combine = min(sz[p],sz[v]);
int l=eu[v]-1 , r=eu[p]-1;
loop(i,1,combine)
{
++l; ++r;
swap(hi[q[l]],hi[q[r]]);
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
loop(i,1,n)
{
lo[i] = hi[i] = i;
}
loop(i,1,n-1)
{
int u,v; cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
brain_DFS(1,1);
cout << Lo << " " << 2*Hi << '\n';
loop(i,1,n){ cout << lo[i] << " "; }
cout << '\n';
loop(i,1,n){ cout << hi[i] << " "; }
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |