#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
int n,u,v,cnt=1,h1[N],h2[N],sz[N],seq[N],lv[N],P[20][N];
ll ans1,ans2;
vector<int> adj[N];
void A1(int u,int p){
for(auto v : adj[u]){
if(v == p) continue;
A1(v,u);
}
if(h1[u] == u){
if(u == 1) swap(h1[u],h1[adj[1][0]]);
else swap(h1[u],h1[p]);
ans1 += 2;
}
}
int size(int u,int p){
sz[u] = 1;
for(auto v : adj[u]) if(v != p) sz[u] += size(v,u);
return sz[u];
}
int find_cen(int u,int p,int all){
for(auto v : adj[u]) if(v != p && sz[v] > n/2) return find_cen(v,u,all);
return u;
}
void dfs(int u,int p,int l){
seq[cnt++] = u;
lv[u] = l;
P[0][u] = p;
for(int i=1;i<=18;i++) P[i][u] = P[i-1][P[i-1][u]];
for(auto v : adj[u]) if(v != p) dfs(v,u,l+1);
}
long long dis(int u,int v){
if(lv[u] < lv[v]) swap(u,v);
long long diff = lv[u] - lv[v], dist = 0;
for(int i=18;i>=0;i--) if((1<<i) & diff) dist += (1<<i), u = P[i][u];
if(u == v) return dist;
for(int i=18;i>=0;i--) if(P[i][u] != P[i][v]) u = P[i][u], v = P[i][v], dist += 2*(1<<i);
return dist + 2;
}
void A2(int u){
size(1,1);
int c = find_cen(1,1,n);
dfs(c,c,1);
for(int i=1;i<=n;i++) h2[seq[i]] = seq[(i+n/2-1)%n+1], ans2 += dis(seq[i],h2[seq[i]]);
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
adj[u].push_back(v), adj[v].push_back(u);
}
for(int i=1;i<=n;i++) h1[i] = i;
A1(1,1);
A2(1);
printf("%lld %lld\n",ans1,ans2);
for(int i=1;i<=n;i++) printf("%d ",h1[i]);
printf("\n");
for(int i=1;i<=n;i++) printf("%d ",h2[i]);
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Village.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2660 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2660 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2664 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2660 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 |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
2924 KB |
Output is correct |
12 |
Correct |
2 ms |
2792 KB |
Output is correct |
13 |
Correct |
2 ms |
2792 KB |
Output is correct |
14 |
Correct |
2 ms |
2788 KB |
Output is correct |
15 |
Correct |
2 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
2 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
2 ms |
2900 KB |
Output is correct |
24 |
Correct |
2 ms |
2900 KB |
Output is correct |
25 |
Correct |
2 ms |
2792 KB |
Output is correct |
26 |
Correct |
2 ms |
2796 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
3 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2660 KB |
Output is correct |
4 |
Correct |
2 ms |
2696 KB |
Output is correct |
5 |
Correct |
2 ms |
2664 KB |
Output is correct |
6 |
Correct |
2 ms |
2660 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2664 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2660 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 |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
2 ms |
2900 KB |
Output is correct |
24 |
Correct |
2 ms |
2900 KB |
Output is correct |
25 |
Correct |
3 ms |
2900 KB |
Output is correct |
26 |
Correct |
2 ms |
2900 KB |
Output is correct |
27 |
Correct |
2 ms |
2900 KB |
Output is correct |
28 |
Correct |
2 ms |
2924 KB |
Output is correct |
29 |
Correct |
2 ms |
2792 KB |
Output is correct |
30 |
Correct |
2 ms |
2792 KB |
Output is correct |
31 |
Correct |
2 ms |
2788 KB |
Output is correct |
32 |
Correct |
2 ms |
2796 KB |
Output is correct |
33 |
Correct |
2 ms |
2900 KB |
Output is correct |
34 |
Correct |
2 ms |
2900 KB |
Output is correct |
35 |
Correct |
2 ms |
2900 KB |
Output is correct |
36 |
Correct |
2 ms |
2900 KB |
Output is correct |
37 |
Correct |
2 ms |
2900 KB |
Output is correct |
38 |
Correct |
2 ms |
2900 KB |
Output is correct |
39 |
Correct |
2 ms |
2900 KB |
Output is correct |
40 |
Correct |
2 ms |
2900 KB |
Output is correct |
41 |
Correct |
2 ms |
2900 KB |
Output is correct |
42 |
Correct |
2 ms |
2792 KB |
Output is correct |
43 |
Correct |
2 ms |
2796 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
3 ms |
2900 KB |
Output is correct |
46 |
Correct |
2 ms |
2796 KB |
Output is correct |
47 |
Correct |
3 ms |
2900 KB |
Output is correct |
48 |
Correct |
118 ms |
16372 KB |
Output is correct |
49 |
Correct |
122 ms |
17620 KB |
Output is correct |
50 |
Correct |
147 ms |
17740 KB |
Output is correct |
51 |
Correct |
82 ms |
14352 KB |
Output is correct |
52 |
Correct |
109 ms |
17512 KB |
Output is correct |
53 |
Correct |
103 ms |
16076 KB |
Output is correct |
54 |
Correct |
50 ms |
12876 KB |
Output is correct |
55 |
Correct |
178 ms |
23520 KB |
Output is correct |
56 |
Correct |
142 ms |
20312 KB |
Output is correct |
57 |
Correct |
157 ms |
19320 KB |
Output is correct |
58 |
Correct |
135 ms |
18496 KB |
Output is correct |
59 |
Correct |
153 ms |
17684 KB |
Output is correct |
60 |
Correct |
105 ms |
17784 KB |
Output is correct |
61 |
Correct |
104 ms |
17892 KB |
Output is correct |
62 |
Correct |
124 ms |
17972 KB |
Output is correct |
63 |
Correct |
95 ms |
17064 KB |
Output is correct |
64 |
Correct |
110 ms |
17888 KB |
Output is correct |
65 |
Correct |
108 ms |
17956 KB |
Output is correct |
66 |
Correct |
86 ms |
17100 KB |
Output is correct |
67 |
Correct |
67 ms |
14044 KB |
Output is correct |
68 |
Correct |
98 ms |
15672 KB |
Output is correct |
69 |
Correct |
103 ms |
18092 KB |
Output is correct |
70 |
Correct |
100 ms |
17096 KB |
Output is correct |
71 |
Correct |
62 ms |
13388 KB |
Output is correct |
72 |
Correct |
80 ms |
14908 KB |
Output is correct |
73 |
Correct |
107 ms |
17996 KB |
Output is correct |
74 |
Correct |
97 ms |
16748 KB |
Output is correct |
75 |
Correct |
128 ms |
17380 KB |
Output is correct |
76 |
Correct |
112 ms |
17508 KB |
Output is correct |
77 |
Correct |
111 ms |
17312 KB |
Output is correct |
78 |
Correct |
57 ms |
12796 KB |
Output is correct |
79 |
Correct |
87 ms |
14344 KB |
Output is correct |
80 |
Correct |
111 ms |
17428 KB |
Output is correct |
81 |
Correct |
116 ms |
17364 KB |
Output is correct |
82 |
Correct |
99 ms |
17840 KB |
Output is correct |