#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, dp[100100], vln[100100], vlx[100100], ansn, ansx, cen, depth[100100];
vector<int> vc[100100], ans;
void dfs(int u, int pa){
for(auto v:vc[u]){
if(v == pa) continue;
dfs(v, u);
if(vln[v] == v){
ansn+=2;
swap(vln[v], vln[u]);
}
dp[u]+=dp[v];
}
}
void dfs2(int u, int pa){
ans.emplace_back(u);
for(auto v:vc[u]){
if(v == pa) continue;
depth[v] = depth[u] + 1;
dfs2(v, u);
}
}
int centroid(int u, int pa){
for(auto v:vc[u]){
if(v == pa) continue;
if(dp[v] > n / 2) return centroid(v, u);
}
return u;
}
int32_t main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for(int i = 1; i<=n; ++i) vln[i] = i;
for(int i = 1; i<=n; ++i) dp[i] = 1;
for(int i = 1; i<n; ++i){
int a, b; cin >> a >> b;
vc[a].push_back(b);
vc[b].push_back(a);
}
dfs(1, -1);
if(vln[1] == 1){
ansn+=2;
swap(vln[1], vln[vc[1][0]]);
}
cen = centroid(1, -1);
dfs2(cen, -1);
for(int i = 1; i<=n; ++i) ansx+=depth[i]*2;
cout << ansn << ' ' << ansx << '\n';
for(int i = 0; i<n; ++i) vlx[ans[(i+n/2)%n]] = ans[i];
for(int i = 1; i<=n; ++i) cout << vln[i] << ' ';
cout << '\n';
for(int i = 1; i<=n; ++i) cout << vlx[i] << ' ';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
2 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4712 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
1 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4900 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
2 ms |
4856 KB |
Output is correct |
28 |
Correct |
2 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4700 KB |
Output is correct |
25 |
Correct |
1 ms |
4700 KB |
Output is correct |
26 |
Correct |
2 ms |
4700 KB |
Output is correct |
27 |
Correct |
1 ms |
4700 KB |
Output is correct |
28 |
Correct |
1 ms |
4700 KB |
Output is correct |
29 |
Correct |
2 ms |
4700 KB |
Output is correct |
30 |
Correct |
1 ms |
4700 KB |
Output is correct |
31 |
Correct |
1 ms |
4696 KB |
Output is correct |
32 |
Correct |
2 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4700 KB |
Output is correct |
34 |
Correct |
2 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4712 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4900 KB |
Output is correct |
42 |
Correct |
1 ms |
4700 KB |
Output is correct |
43 |
Correct |
2 ms |
4700 KB |
Output is correct |
44 |
Correct |
2 ms |
4856 KB |
Output is correct |
45 |
Correct |
2 ms |
4700 KB |
Output is correct |
46 |
Correct |
1 ms |
4700 KB |
Output is correct |
47 |
Correct |
1 ms |
4700 KB |
Output is correct |
48 |
Correct |
37 ms |
10960 KB |
Output is correct |
49 |
Correct |
42 ms |
11592 KB |
Output is correct |
50 |
Correct |
47 ms |
11972 KB |
Output is correct |
51 |
Correct |
39 ms |
10184 KB |
Output is correct |
52 |
Correct |
44 ms |
11592 KB |
Output is correct |
53 |
Correct |
36 ms |
10948 KB |
Output is correct |
54 |
Correct |
26 ms |
11212 KB |
Output is correct |
55 |
Correct |
52 ms |
18628 KB |
Output is correct |
56 |
Correct |
52 ms |
15048 KB |
Output is correct |
57 |
Correct |
52 ms |
13772 KB |
Output is correct |
58 |
Correct |
45 ms |
12796 KB |
Output is correct |
59 |
Correct |
41 ms |
11724 KB |
Output is correct |
60 |
Correct |
44 ms |
11844 KB |
Output is correct |
61 |
Correct |
39 ms |
11964 KB |
Output is correct |
62 |
Correct |
38 ms |
12148 KB |
Output is correct |
63 |
Correct |
34 ms |
11616 KB |
Output is correct |
64 |
Correct |
38 ms |
11984 KB |
Output is correct |
65 |
Correct |
39 ms |
12232 KB |
Output is correct |
66 |
Correct |
35 ms |
11468 KB |
Output is correct |
67 |
Correct |
26 ms |
9928 KB |
Output is correct |
68 |
Correct |
32 ms |
10960 KB |
Output is correct |
69 |
Correct |
37 ms |
12232 KB |
Output is correct |
70 |
Correct |
33 ms |
11616 KB |
Output is correct |
71 |
Correct |
26 ms |
9936 KB |
Output is correct |
72 |
Correct |
28 ms |
10536 KB |
Output is correct |
73 |
Correct |
38 ms |
12240 KB |
Output is correct |
74 |
Correct |
33 ms |
11464 KB |
Output is correct |
75 |
Correct |
42 ms |
12116 KB |
Output is correct |
76 |
Correct |
45 ms |
11588 KB |
Output is correct |
77 |
Correct |
38 ms |
12100 KB |
Output is correct |
78 |
Correct |
33 ms |
9412 KB |
Output is correct |
79 |
Correct |
30 ms |
10388 KB |
Output is correct |
80 |
Correct |
42 ms |
12244 KB |
Output is correct |
81 |
Correct |
45 ms |
12232 KB |
Output is correct |
82 |
Correct |
35 ms |
11732 KB |
Output is correct |