#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3fm6nhZ ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
#define all(a) a.begin(),a.end()
signed main(){
_3fm6nhZ;
int n;
cin>>n;
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u-=1,v-=1;
adj[u].pb(v);
adj[v].pb(u);
}
int ans=0;
vi ert(n);
auto dfs=[&](auto self,int v,int par)->int{
vi rbts;
for(auto u:adj[v]){
if(u==par) continue;
int e=self(self,u,v);
if(e) rbts.pb(u);
}
if(sz(rbts)==0) return 1;
for(int i=0;i<sz(rbts)-sz(rbts)%2;i+=2){
ert[rbts[i]]=rbts[i+1];
ert[rbts[i+1]]=rbts[i];
ans+=4;
}
if(sz(rbts)%2){
ert[v]=rbts.back();
ert[rbts.back()]=v;
ans+=2;
}else{
ert[v]=rbts.back();
ert[ert[rbts.back()]]=v;
}
return 0;
};
int e=dfs(dfs,0,-1);
if(e){
int v=0;
int u=adj[0][0];
ans+=2;
rep(s,n){
if(ert[s]==u){
ert[v]=u;
ert[s]=0;
break;
}
}
}
int ans1=0;
vi chs(n);
auto pre_rfs=[&](auto self,int v,int par)->void{
chs[v]=1;
for(auto u:adj[v]){
if(u==par) continue;
self(self,u,v);
chs[v]+=chs[u];
ans1+=2*min(chs[u],n-chs[u]);
}
};
pre_rfs(pre_rfs,0,-1);
auto rfs=[&](auto self,int v,int par)->int{
for(auto u:adj[v]){
if(u==par) continue;
if(2*chs[u]>=n){
return self(self,u,v);
}
}
return v;
};
int rt=rfs(rfs,0,-1);
chs=vi(n,0);
ans1=0;
pre_rfs(pre_rfs,rt,-1);
vi way;
auto add=[&](auto self,int v,int par)->void{
way.pb(v);
for(auto u:adj[v]){
if(u==par) continue;
self(self,u,v);
}
};
sort(all(adj[rt]),[&](int l,int r){
return chs[l]<chs[r];
});
vec(vi) rbts;
for(auto u:adj[rt]){
way.clear();
add(add,u,rt);
rbts.pb(way);
}
set<int> st;
rep(i,n){
st.insert(i);
}
vi usd(n);
vi ert1(n);
auto ask=[&](auto self,int v,int par)->void{
if(!sz(st)){
cout<<"asdasdasdhi\n";
exit(0);
}
assert(sz(st));
if(st.find(rt)!=st.end()){
usd[rt]=1;
ert1[rt]=v;
st.erase(st.find(rt));
}else{
auto it=st.begin();
usd[*it]=1;
ert1[*it]=v;
st.erase(it);
}
for(auto u:adj[v]){
if(u==par) continue;
self(self,u,v);
}
};
per(i,sz(adj[rt])){
for(auto v:rbts[i]){
if(!usd[v]){
st.erase(st.find(v));
}
}
int u=adj[rt][i];
ask(ask,u,rt);
for(auto v:rbts[i]){
if(!usd[v]){
st.insert(v);
}
}
}
assert(sz(st));
ert1[*st.begin()]=rt;
cout<<ans<<" "<<ans1<<"\n";
rep(i,n){
cout<<ert[i]+1<<" ";
}
print();
rep(i,n){
cout<<ert1[i]+1<<" ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Expected integer, but "asdasdasdhi" found |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Incorrect |
1 ms |
468 KB |
Expected integer, but "asdasdasdhi" found |
23 |
Halted |
0 ms |
0 KB |
- |