This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//author : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
const int N = 1e5 + 7;
const int M = 20;
const int inf = 1e9 + 7;
int n , lift[N][M] , depth[N];
vector < int > tree[N];
void dfs(int node , int par){
depth[node] = depth[par] + 1;
lift[node][0] = par;
for(auto itr : tree[node]){
if(itr != par){
dfs(itr , node);
}
}
}
int find_lca(int a , int b){
if(depth[a] < depth[b])swap(a , b);
int diff = depth[a] - depth[b];
for(int i = 0;i<M;i++){
if(diff & (1 << i)){
a = lift[a][i];
}
}
if(a == b)return a;
for(int i = M-1;i>=0;i--){
if(lift[a][i] != lift[b][i]){
a = lift[a][i] , b = lift[b][i];
}
}
return lift[a][0];
}
int find_dist(int a , int b){
int lc = find_lca(a , b);
return depth[a] + depth[b] - 2 * depth[lc];
}
void solve(){
cin >> n;
for(int i = 1;i<n;i++){
int a,b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
dfs(1 , 0);
for(int i = 1;i<M;i++){
for(int j = 1;j<=n;j++){
lift[j][i] = lift[lift[j][i-1]][i-1];
}
}
vector < int > perm(n) , v_mn , v_mx;
iota(all(perm) , 1);
int mn_ans = inf , mx_ans = -inf;
do{
bool flag = 0;
for(int i = 0;i<n;i++){
if(perm[i] == (i+1))flag = 1;
}
if(flag)continue;
int cost = 0;
for(int i = 0;i<n;i++){
cost += find_dist(i+1 , perm[i]);
}
if(cost < mn_ans){
mn_ans = cost;
v_mn = perm;
}
if(cost > mx_ans){
mx_ans = cost;
v_mx = perm;
}
} while(next_permutation(all(perm)));
cout << mn_ans << " " << mx_ans << endl;
for(auto itr : v_mn)cout << itr << " ";cout << endl;
for(auto itr : v_mx)cout << itr << " ";cout << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
Compilation message (stderr)
Village.cpp: In function 'void solve()':
Village.cpp:77:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
77 | for(auto itr : v_mn)cout << itr << " ";cout << endl;
| ^~~
Village.cpp:77:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
77 | for(auto itr : v_mn)cout << itr << " ";cout << endl;
| ^~~~
Village.cpp:78:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
78 | for(auto itr : v_mx)cout << itr << " ";cout << endl;
| ^~~
Village.cpp:78:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
78 | for(auto itr : v_mx)cout << itr << " ";cout << endl;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |