#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<vector<int> > graph;
vector<int> match,prem;
vector<vector<int> > dp;
vector<int> sz,ord;
int n;
typedef long long ll;
ll max_cnt=0;
void dfs_min(int node,int parent){
sz[node]++;
for(auto x:graph[node]){
if(x==parent) continue;
dfs_min(x,node);
sz[node]+=sz[x];
dp[node][0]+=max(dp[x][0],dp[x][1]);
}
for(auto x:graph[node]){
if(x==parent) continue;
if(dp[node][1]<dp[node][0]-max(dp[x][0],dp[x][1])+dp[x][0]+1){
dp[node][1]=dp[node][0]-max(dp[x][0],dp[x][1])+dp[x][0]+1;
prem[node]=x;
}
}
}
void dfs_match(int node,int parent,bool flag){
if(flag||dp[node][0]>=dp[node][1]){
for(auto x:graph[node]){
if(x==parent) continue;
dfs_match(x,node,0);
}
}
else{
match[node]=prem[node];
match[prem[node]]=node;
for(auto x:graph[node]){
if(x==parent) continue;
if(x==prem[node]) dfs_match(x,node,1);
else dfs_match(x,node,0);
}
}
}
int dfs_cent(int node,int parent){
for(auto x:graph[node]){
if(x==parent) continue;
if(sz[x]>n/2) return dfs_cent(x,node);
}
return node;
}
void dfs_sz(int node,int parent){
sz[node]=1;
for(auto x:graph[node]){
if(x==parent) continue;
dfs_sz(x,node);
sz[node]+=sz[x];
}
if(parent!=0) max_cnt+=2*min(1ll*sz[node],1ll*(n-sz[node]));
}
void dfs_ord(int node,int parent){
if(parent!=0) ord.push_back(node);
for(auto x:graph[node]){
if(x==parent) continue;
dfs_ord(x,node);
}
}
int main(){
cin>>n;
graph.resize(n+1);
prem.resize(n+1);
sz.resize(n+1);
int a,b;
for(int i=1;i<n;i++){
cin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
}
dp.resize(n+1,vector<int>(2));
match.resize(n+1,-1);
dfs_min(1,0);
dfs_match(1,0,0);
vector<int> ans_min(n+1);
for(int i=1;i<=n;i++){
if(match[i]!=-1){
ans_min[i]=match[i];
}
else ans_min[i]=i;
}
int min_cnt=max(dp[1][1],dp[1][0])*2;
for(int i=1;i<=n;i++){
if(match[i]==-1){
swap(ans_min[i],ans_min[graph[i][0]]),min_cnt+=2;
}
}
int cent=dfs_cent(1,0);
dfs_sz(cent,0);
sort(graph[cent].begin(),graph[cent].end(),[&](int a,int b){return sz[a]>sz[b];});
dfs_ord(cent,0);
vector<int> ans_max(n+1);
for(int i=1;i<=n;i++) ans_max[i]=i;
if(n&1){
for(int i=0;i<n/2;i++) swap(ans_max[ord[i]],ans_max[ord[i+n/2]]);
swap(ans_max[cent],ans_max[1]);
}
else{
ord.push_back(cent);
for(int i=0;i<n/2;i++) swap(ans_max[ord[i]],ans_max[ord[i+n/2]]);
}
cout<<min_cnt<<" "<<max_cnt<<"\n";
for(int i=1;i<=n;i++) cout<<ans_min[i]<<" \n"[i==n];
for(int i=1;i<=n;i++) cout<<ans_max[i]<<" \n"[i==n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Partially correct |
0 ms |
348 KB |
Partially correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
432 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
544 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Partially correct |
0 ms |
348 KB |
Partially correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
432 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
2 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
544 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
78 ms |
14792 KB |
Output is correct |
49 |
Correct |
88 ms |
16288 KB |
Output is correct |
50 |
Correct |
89 ms |
16332 KB |
Output is correct |
51 |
Correct |
66 ms |
12756 KB |
Output is correct |
52 |
Correct |
102 ms |
16008 KB |
Output is correct |
53 |
Correct |
76 ms |
14536 KB |
Output is correct |
54 |
Correct |
53 ms |
11744 KB |
Output is correct |
55 |
Correct |
122 ms |
23944 KB |
Output is correct |
56 |
Correct |
101 ms |
19912 KB |
Output is correct |
57 |
Correct |
97 ms |
18380 KB |
Output is correct |
58 |
Correct |
99 ms |
17356 KB |
Output is correct |
59 |
Correct |
127 ms |
16480 KB |
Output is correct |
60 |
Correct |
82 ms |
16448 KB |
Output is correct |
61 |
Correct |
79 ms |
16580 KB |
Output is correct |
62 |
Correct |
80 ms |
16472 KB |
Output is correct |
63 |
Correct |
82 ms |
15680 KB |
Output is correct |
64 |
Correct |
81 ms |
16588 KB |
Output is correct |
65 |
Correct |
79 ms |
16716 KB |
Output is correct |
66 |
Correct |
72 ms |
15568 KB |
Output is correct |
67 |
Correct |
57 ms |
12236 KB |
Output is correct |
68 |
Correct |
83 ms |
14128 KB |
Output is correct |
69 |
Correct |
82 ms |
16664 KB |
Output is correct |
70 |
Correct |
77 ms |
15568 KB |
Output is correct |
71 |
Correct |
52 ms |
11720 KB |
Output is correct |
72 |
Correct |
61 ms |
13264 KB |
Output is correct |
73 |
Correct |
77 ms |
16596 KB |
Output is correct |
74 |
Correct |
71 ms |
15212 KB |
Output is correct |
75 |
Correct |
91 ms |
16136 KB |
Output is correct |
76 |
Correct |
85 ms |
16072 KB |
Output is correct |
77 |
Correct |
88 ms |
15900 KB |
Output is correct |
78 |
Correct |
54 ms |
11004 KB |
Output is correct |
79 |
Correct |
61 ms |
12864 KB |
Output is correct |
80 |
Correct |
87 ms |
16076 KB |
Output is correct |
81 |
Correct |
83 ms |
15816 KB |
Output is correct |
82 |
Correct |
85 ms |
16304 KB |
Output is correct |