#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
vector<int>adj[maxn];
int n,m,sz[maxn],linkmin[maxn],linkmax[maxn];
vector<vector<int>>allk;
long long res=0;
void dfs(int u,int par=0)
{
sz[u]=1;
for(auto x:adj[u]){
if(x!=par){
dfs(x,u);
sz[u]+=sz[x];
}
}
}
int findcen(int u,int ted,int par=0){
//cout<<u<<" "<<ted<<" "<<par<<" "<<sz[u]<<endl;
for(auto x:adj[u]){
if(x!=par&&sz[x]*2>=ted){
return findcen(x,ted,u);
}
}
//cout<<u<<endl;
return u;
}
int finds(){
dfs(1);
//cout<<"salam"<<endl;
return findcen(1,sz[1]);
}
int solve(int u,int rishe,int par=0){
if(u!=rishe){
allk.back().push_back(u);
}
int ret=1;
for(auto x:adj[u]){
if(x!=par){
if(u==rishe){
allk.push_back({});
}
int z=solve(x,rishe,u);
res+=2*z;
ret+=z;
}
}
return ret;
}
long long res2=0;
int solve2(int u,int par=0){
vector<int>v;
v.push_back(u);
for(auto x:adj[u]){
if(x!=par){
if(solve2(x,u)==1){
v.push_back(x);
}
}
}
if((int)v.size()==1){
return 1;
}
int sz=(int)v.size();
res2+=sz*2-2;
for(int j=0;j<sz;j++){
linkmin[v[j]]=v[(j+1)%sz];
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int r=finds();
//cout<<"salam"<<endl;
solve(r,r);
int f=solve2(1);
if(f==1){
int wtf=adj[1][0];
for(int j=1;j<=n;j++){
if(linkmin[j]==wtf){
wtf=j;
break;
}
}
linkmin[wtf]=1;
linkmin[1]=adj[1][0];
res2+=2;
}
cout<<res2<<" "<<res<<"\n";
for(int i=1;i<=n;i++){
cout<<linkmin[i]<<" ";
}
cout<<"\n";
set<pair<int,int>>st;
for(int i=0;i<(int)allk.size();i++){
st.insert(make_pair((int)allk[i].size(),i));
}
//cout<<"salam"<<endl;
while((int)st.size()>1){
auto x=*st.rbegin();
st.erase(x);
auto y=*st.rbegin();
st.erase(y);
linkmax[allk[y.second].back()]=allk[x.second].back();
linkmax[allk[x.second].back()]=allk[y.second].back();
allk[x.second].pop_back();
allk[y.second].pop_back();
x.first--;
y.first--;
if(x.first>0){
st.insert(x);
}
if(y.first>0){
st.insert(y);
}
}
if((int)st.size()>0){
auto x=*st.rbegin();
int z=allk[x.second].back();
linkmax[z]=r;
linkmax[r]=z;
}
else{
if(r!=1){
linkmax[r]=1;
linkmax[linkmax[1]]=r;
}
else{
linkmax[r]=2;
linkmax[linkmax[2]]=r;
}
}
for(int i=1;i<=n;i++){
cout<<linkmax[i]<<" ";
}
cout<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2672 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2680 KB |
Output is correct |
7 |
Correct |
2 ms |
2808 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2680 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2680 KB |
Output is correct |
17 |
Correct |
2 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2684 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2680 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
3 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2672 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2680 KB |
Output is correct |
24 |
Correct |
2 ms |
2808 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2680 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2680 KB |
Output is correct |
34 |
Correct |
2 ms |
2680 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
3 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2684 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
2 ms |
2644 KB |
Output is correct |
40 |
Correct |
2 ms |
2644 KB |
Output is correct |
41 |
Correct |
2 ms |
2644 KB |
Output is correct |
42 |
Correct |
2 ms |
2644 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2680 KB |
Output is correct |
46 |
Correct |
2 ms |
2644 KB |
Output is correct |
47 |
Correct |
2 ms |
2680 KB |
Output is correct |
48 |
Correct |
52 ms |
9300 KB |
Output is correct |
49 |
Correct |
53 ms |
9972 KB |
Output is correct |
50 |
Correct |
52 ms |
9928 KB |
Output is correct |
51 |
Correct |
40 ms |
8396 KB |
Output is correct |
52 |
Correct |
50 ms |
9844 KB |
Output is correct |
53 |
Correct |
55 ms |
9304 KB |
Output is correct |
54 |
Correct |
33 ms |
12492 KB |
Output is correct |
55 |
Correct |
72 ms |
23336 KB |
Output is correct |
56 |
Correct |
72 ms |
16044 KB |
Output is correct |
57 |
Correct |
63 ms |
13900 KB |
Output is correct |
58 |
Correct |
67 ms |
11984 KB |
Output is correct |
59 |
Correct |
57 ms |
10040 KB |
Output is correct |
60 |
Correct |
61 ms |
19740 KB |
Output is correct |
61 |
Correct |
56 ms |
12036 KB |
Output is correct |
62 |
Correct |
47 ms |
10260 KB |
Output is correct |
63 |
Correct |
44 ms |
9912 KB |
Output is correct |
64 |
Correct |
53 ms |
10216 KB |
Output is correct |
65 |
Correct |
45 ms |
10412 KB |
Output is correct |
66 |
Correct |
45 ms |
10140 KB |
Output is correct |
67 |
Correct |
39 ms |
9476 KB |
Output is correct |
68 |
Correct |
41 ms |
9156 KB |
Output is correct |
69 |
Correct |
49 ms |
10440 KB |
Output is correct |
70 |
Correct |
44 ms |
9936 KB |
Output is correct |
71 |
Correct |
32 ms |
8012 KB |
Output is correct |
72 |
Correct |
36 ms |
8692 KB |
Output is correct |
73 |
Correct |
48 ms |
10488 KB |
Output is correct |
74 |
Correct |
46 ms |
9776 KB |
Output is correct |
75 |
Correct |
50 ms |
9712 KB |
Output is correct |
76 |
Correct |
47 ms |
9828 KB |
Output is correct |
77 |
Correct |
59 ms |
10164 KB |
Output is correct |
78 |
Correct |
32 ms |
7628 KB |
Output is correct |
79 |
Correct |
36 ms |
8484 KB |
Output is correct |
80 |
Correct |
49 ms |
9808 KB |
Output is correct |
81 |
Correct |
46 ms |
10060 KB |
Output is correct |
82 |
Correct |
46 ms |
10128 KB |
Output is correct |