#include<bits/stdc++.h>
using namespace std ;
const int MX = 1e5 + 5 , INF = 1e9 + 5 , LG = 25 ;
int dp[MX][2] , mn[MX] ;
vector<int> adj[MX] ;
vector<pair<int,int> > pick[MX][2] ;
void dfs(int s , int p)
{
int sum = 0 , good = 0 , leaf = 1 , rd = 0 , pos = 0 , idx = 0 ;
dp[s][0] = 0 , dp[s][1] = INF ;
for(int t : adj[s])
{
if(t==p) continue ;
dfs(t,s) , leaf = 0 ;
dp[s][0] = min(dp[s][0] + dp[t][1] , INF) , pick[s][0].push_back({t,1}) ;
if((dp[t][0]+2)<=dp[t][1])
{
sum += dp[t][0] + 2 , pick[s][1].push_back({t,0}) , ++pos ;
good = 1 ;
}
else
{
if(dp[t][0]<dp[t][1])
{
rd = t ;
idx = pos ;
}
sum += dp[t][1] , pick[s][1].push_back({t,1}) , ++pos;
}
}
if(!leaf)
{
if(!good)
{
if(rd)
{
pick[s][1].erase(pick[s][1].begin()+idx) ;
pick[s][1].push_back({rd,0}) ;
sum -= dp[rd][1] ;
sum += dp[rd][0] + 2 ;
}
else
{
pick[s][1][0].second = 2 ;
sum += 2 ;
}
}
dp[s][1] = sum ;
}
}
void build(int s , int c)
{
if(c==2) c = 1 ;
for(pair<int,int> p : pick[s][c])
{
build(p.first,p.second) ;
if(p.second!=1) swap(mn[p.first],mn[s]) ;
}
}
struct centroid_tree
{
long long cst = 0 ;
int N = 0 ;
vector<int> S ;
vector<vector<int> > adj ;
vector<int> sub , centroid , mx , par ;
void init(int n , vector<int> f[])
{
N = n ;
adj = vector<vector<int> > (n+2) ;
sub = vector<int> (n+2,0) ;
mx = vector<int> (n+2,0) ;
par = vector<int> (n+2,0) ;
centroid = vector<int> (n+2,0) ;
for(int i = 1 ; i <= n ; ++i)
{
mx[i] = i ;
for(int j : f[i])
{
adj[i].push_back(j) ;
}
}
find_ans() ;
}
void find_siz(int s , int p)
{
sub[s] = 1 ;
for(int t : adj[s])
{
if(t==p) continue ;
if(centroid[t]) continue ;
find_siz(t,s) ;
sub[s] += sub[t] ;
}
for(int t : adj[s])
{
if(t==p) continue ;
cst += min(sub[t],N-sub[t]) ;
}
}
int find_centroid(int s , int p , int n)
{
for(int t : adj[s])
{
if(t==p) continue ;
if(centroid[t]) continue ;
if(sub[t]>(n/2)) return find_centroid(t,s,n) ;
}
return s ;
}
void topsort(int s , int p)
{
for(int t : adj[s])
{
if(t==p) continue ;
topsort(t,s) ;
}
S.push_back(s) ;
}
void find_ans()
{
find_siz(1,0) ;
int C = find_centroid(1,0,sub[1]) ;
topsort(C,0) ;
reverse(S.begin(),S.end()) ;
for(int i = 0 ; i < (N/2) ; ++i)
{
swap(mx[S[i]],mx[S[(i+(N/2))%N]]) ;
}
if(N&1) swap(mx[S[N/2]],mx[S[N-1]]) ;
}
};
int main()
{
int n ;
scanf("%d",&n) ;
for(int i = 1 ; i < n ; ++i)
{
int x , y ;
scanf("%d%d",&x,&y) ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
for(int i = 1 ; i <= n ; ++i)
{
mn[i] = i ;
}
dfs(1,0) ;
build(1,1) ;
centroid_tree C ;
C.init(n,adj) ;
printf("%d %lld\n",dp[1][1],2*C.cst) ;
for(int i = 1 ; i <= n ; ++i)
{
printf("%d ",mn[i]) ;
}
printf("\n") ;
for(int i = 1 ; i <= n ; ++i)
{
printf("%d ",C.mx[i]) ;
}
printf("\n") ;
return 0 ;
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:145:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | scanf("%d",&n) ;
| ~~~~~^~~~~~~~~
Village.cpp:149:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | scanf("%d%d",&x,&y) ;
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7236 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7260 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
3 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7436 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
5 ms |
7508 KB |
Output is correct |
5 |
Correct |
6 ms |
7508 KB |
Output is correct |
6 |
Correct |
6 ms |
7508 KB |
Output is correct |
7 |
Correct |
5 ms |
7608 KB |
Output is correct |
8 |
Correct |
5 ms |
7508 KB |
Output is correct |
9 |
Correct |
4 ms |
7508 KB |
Output is correct |
10 |
Correct |
6 ms |
7456 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
5 ms |
7508 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7508 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7508 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7508 KB |
Output is correct |
19 |
Correct |
4 ms |
7508 KB |
Output is correct |
20 |
Correct |
4 ms |
7508 KB |
Output is correct |
21 |
Correct |
4 ms |
7508 KB |
Output is correct |
22 |
Correct |
5 ms |
7508 KB |
Output is correct |
23 |
Correct |
5 ms |
7508 KB |
Output is correct |
24 |
Correct |
5 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7508 KB |
Output is correct |
27 |
Correct |
4 ms |
7380 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
4 ms |
7508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7236 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7260 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
3 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7252 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7436 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
5 ms |
7508 KB |
Output is correct |
22 |
Correct |
6 ms |
7508 KB |
Output is correct |
23 |
Correct |
6 ms |
7508 KB |
Output is correct |
24 |
Correct |
5 ms |
7608 KB |
Output is correct |
25 |
Correct |
5 ms |
7508 KB |
Output is correct |
26 |
Correct |
4 ms |
7508 KB |
Output is correct |
27 |
Correct |
6 ms |
7456 KB |
Output is correct |
28 |
Correct |
5 ms |
7508 KB |
Output is correct |
29 |
Correct |
5 ms |
7508 KB |
Output is correct |
30 |
Correct |
4 ms |
7508 KB |
Output is correct |
31 |
Correct |
4 ms |
7508 KB |
Output is correct |
32 |
Correct |
4 ms |
7380 KB |
Output is correct |
33 |
Correct |
4 ms |
7508 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
5 ms |
7508 KB |
Output is correct |
36 |
Correct |
4 ms |
7508 KB |
Output is correct |
37 |
Correct |
4 ms |
7508 KB |
Output is correct |
38 |
Correct |
4 ms |
7508 KB |
Output is correct |
39 |
Correct |
5 ms |
7508 KB |
Output is correct |
40 |
Correct |
5 ms |
7508 KB |
Output is correct |
41 |
Correct |
5 ms |
7380 KB |
Output is correct |
42 |
Correct |
4 ms |
7380 KB |
Output is correct |
43 |
Correct |
4 ms |
7508 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
5 ms |
7508 KB |
Output is correct |
46 |
Correct |
5 ms |
7508 KB |
Output is correct |
47 |
Correct |
4 ms |
7508 KB |
Output is correct |
48 |
Correct |
96 ms |
22696 KB |
Output is correct |
49 |
Correct |
109 ms |
24300 KB |
Output is correct |
50 |
Correct |
110 ms |
24252 KB |
Output is correct |
51 |
Correct |
83 ms |
20580 KB |
Output is correct |
52 |
Correct |
123 ms |
24076 KB |
Output is correct |
53 |
Correct |
109 ms |
22340 KB |
Output is correct |
54 |
Correct |
59 ms |
22608 KB |
Output is correct |
55 |
Correct |
143 ms |
38732 KB |
Output is correct |
56 |
Correct |
147 ms |
30572 KB |
Output is correct |
57 |
Correct |
143 ms |
28488 KB |
Output is correct |
58 |
Correct |
130 ms |
26716 KB |
Output is correct |
59 |
Correct |
108 ms |
24124 KB |
Output is correct |
60 |
Correct |
77 ms |
23100 KB |
Output is correct |
61 |
Correct |
79 ms |
23248 KB |
Output is correct |
62 |
Correct |
81 ms |
23228 KB |
Output is correct |
63 |
Correct |
84 ms |
22284 KB |
Output is correct |
64 |
Correct |
90 ms |
23692 KB |
Output is correct |
65 |
Correct |
83 ms |
23752 KB |
Output is correct |
66 |
Correct |
79 ms |
22532 KB |
Output is correct |
67 |
Correct |
60 ms |
19168 KB |
Output is correct |
68 |
Correct |
68 ms |
21224 KB |
Output is correct |
69 |
Correct |
81 ms |
23848 KB |
Output is correct |
70 |
Correct |
78 ms |
22748 KB |
Output is correct |
71 |
Correct |
54 ms |
18628 KB |
Output is correct |
72 |
Correct |
65 ms |
20372 KB |
Output is correct |
73 |
Correct |
88 ms |
23908 KB |
Output is correct |
74 |
Correct |
74 ms |
22384 KB |
Output is correct |
75 |
Correct |
117 ms |
23460 KB |
Output is correct |
76 |
Correct |
104 ms |
23416 KB |
Output is correct |
77 |
Correct |
88 ms |
23552 KB |
Output is correct |
78 |
Correct |
60 ms |
18108 KB |
Output is correct |
79 |
Correct |
75 ms |
19536 KB |
Output is correct |
80 |
Correct |
108 ms |
23504 KB |
Output is correct |
81 |
Correct |
91 ms |
23812 KB |
Output is correct |
82 |
Correct |
84 ms |
22708 KB |
Output is correct |