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;
using ll=long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin>>n;
vector<vector<int>>g(n);
for(int i=1,u,v;i<n;++i){
cin>>u>>v;
--u,--v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int>ord,sz(n);
ll cnt=0;
function<void(int,int)>dfs=[&](int u, int p){
sz[u]=1;
ord.push_back(u);
for(const int&i:g[u])
if(i!=p){
dfs(i,u);
sz[u]+=sz[i];
cnt+=min(sz[i],n-sz[i]);
}
};
dfs(0,0);
vector<int>ans(n);
for(int i=0;i<n;++i){
int u=ord[i];
int v=ord[(i+n/2)%n];
ans[v]=u;
}
cout<<2*cnt<<'\n';
for(int i=0;i<n;++i)cout<<ans[i]+1<<' ';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |