#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];
});
set<pii> st;
vec(vi) rbts;
rep(i,sz(adj[rt])){
int u=adj[rt][i];
way.clear();
add(add,u,rt);
for(auto x:way)st.insert(pii(i,x));
rbts.pb(way);
}
st.insert(pii(-1,rt));
vi usd(n);
vi ert1(n);
auto ask=[&](auto self,int v,int par)->void{
assert(sz(st));
auto it=st.begin();
// print((*it).se);
ert1[(*it).se]=v;
usd[(*it).se]=1;
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]){
// print(st.find(pii(i,v))!=st.end());
st.erase(st.find(pii(i,v)));
}
}
int u=adj[rt][i];
ask(ask,u,rt);
for(auto v:rbts[i]){
if(!usd[v]){
st.insert(pii(i,v));
}
}
}
assert(sz(st));
ert1[(*st.begin()).se]=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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
312 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 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 |
0 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 |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
159 ms |
16520 KB |
Output is correct |
49 |
Correct |
146 ms |
18132 KB |
Output is correct |
50 |
Correct |
164 ms |
18104 KB |
Output is correct |
51 |
Correct |
104 ms |
14160 KB |
Output is correct |
52 |
Correct |
136 ms |
17696 KB |
Output is correct |
53 |
Correct |
159 ms |
16124 KB |
Output is correct |
54 |
Correct |
66 ms |
15992 KB |
Output is correct |
55 |
Correct |
219 ms |
32672 KB |
Output is correct |
56 |
Correct |
168 ms |
25160 KB |
Output is correct |
57 |
Correct |
213 ms |
22712 KB |
Output is correct |
58 |
Correct |
160 ms |
20592 KB |
Output is correct |
59 |
Correct |
180 ms |
18536 KB |
Output is correct |
60 |
Correct |
93 ms |
22572 KB |
Output is correct |
61 |
Correct |
148 ms |
19008 KB |
Output is correct |
62 |
Correct |
120 ms |
18484 KB |
Output is correct |
63 |
Correct |
163 ms |
17336 KB |
Output is correct |
64 |
Correct |
123 ms |
18376 KB |
Output is correct |
65 |
Correct |
147 ms |
18668 KB |
Output is correct |
66 |
Correct |
125 ms |
17388 KB |
Output is correct |
67 |
Correct |
81 ms |
14152 KB |
Output is correct |
68 |
Correct |
136 ms |
15668 KB |
Output is correct |
69 |
Correct |
134 ms |
18608 KB |
Output is correct |
70 |
Correct |
128 ms |
17388 KB |
Output is correct |
71 |
Correct |
75 ms |
13008 KB |
Output is correct |
72 |
Correct |
100 ms |
14692 KB |
Output is correct |
73 |
Correct |
127 ms |
18620 KB |
Output is correct |
74 |
Correct |
109 ms |
16992 KB |
Output is correct |
75 |
Correct |
201 ms |
18508 KB |
Output is correct |
76 |
Correct |
151 ms |
17944 KB |
Output is correct |
77 |
Correct |
142 ms |
17664 KB |
Output is correct |
78 |
Correct |
73 ms |
12288 KB |
Output is correct |
79 |
Correct |
89 ms |
14172 KB |
Output is correct |
80 |
Correct |
221 ms |
18616 KB |
Output is correct |
81 |
Correct |
114 ms |
17636 KB |
Output is correct |
82 |
Correct |
147 ms |
17964 KB |
Output is correct |