#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
using namespace std;
const int N = 1e5 + 2;
const ll mod = 1e9 + 7;
vector<int> adj[N];
int ans[N], sol;
int sz[N], dep[N], n;
vector<int> order;
void Dfs(int s, int e) {
for (auto u : adj[s]) {
if (u == e) continue;
Dfs(u, s);
if (ans[u] == u) {
swap(ans[u], ans[s]);
sol += 2;
}
}
}
void Dfsb(int s, int e) {
sz[s] = 1;
for (auto u : adj[s]) {
if (u == e) continue;
Dfsb(u, s);
sz[s] += sz[u];
}
}
int Dfs1b(int s, int e) {
for (auto u : adj[s]) {
if (u == e) continue;
if (2 * sz[u] > n) return Dfs1b(u, s);
}
return s;
}
void Dfs2b(int s, int e) {
order.push_back(s);
for (auto u : adj[s]) {
if (u == e) continue;
dep[u] = dep[s] + 1;
Dfs2b(u, s);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) ans[i] = i;
Dfs(1, 0);
if (ans[1] == 1) {
swap(ans[1], ans[adj[1][0]]);
sol += 2;
}
ll solmn = sol;
vector<int> ansmn;
for (int i = 1; i <= n; i++) {
ansmn.push_back(ans[i]);
ans[i] = 0;
}
Dfsb(1, 0);
int cen = Dfs1b(1, 0);
Dfs2b(cen, 0);
sol = 0;
for (int i = 0; i < n; i++) {
int x = order[i];
int y = order[(i + n / 2) % n];
ans[x] = y;
sol += dep[x] + dep[y];
}
cout << solmn << sp << sol << en;
for (int i : ansmn) cout << i << sp;
cout << en;
for (int i = 1; i <= n; i++) cout << ans[i] << sp;
cout << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2676 KB |
Output is correct |
7 |
Correct |
2 ms |
2676 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2676 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2676 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 |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2752 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 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 |
2680 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2676 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 |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
3 ms |
2680 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 |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2676 KB |
Output is correct |
7 |
Correct |
2 ms |
2676 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2676 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2676 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2676 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 |
2772 KB |
Output is correct |
25 |
Correct |
3 ms |
2752 KB |
Output is correct |
26 |
Correct |
2 ms |
2676 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
2 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2680 KB |
Output is correct |
39 |
Correct |
3 ms |
2644 KB |
Output is correct |
40 |
Correct |
2 ms |
2644 KB |
Output is correct |
41 |
Correct |
2 ms |
2676 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 |
2644 KB |
Output is correct |
46 |
Correct |
2 ms |
2644 KB |
Output is correct |
47 |
Correct |
3 ms |
2680 KB |
Output is correct |
48 |
Correct |
64 ms |
9560 KB |
Output is correct |
49 |
Correct |
83 ms |
10268 KB |
Output is correct |
50 |
Correct |
64 ms |
10364 KB |
Output is correct |
51 |
Correct |
47 ms |
8712 KB |
Output is correct |
52 |
Correct |
62 ms |
10152 KB |
Output is correct |
53 |
Correct |
58 ms |
9552 KB |
Output is correct |
54 |
Correct |
34 ms |
9932 KB |
Output is correct |
55 |
Partially correct |
86 ms |
17680 KB |
Partially correct |
56 |
Partially correct |
95 ms |
13784 KB |
Partially correct |
57 |
Correct |
71 ms |
12552 KB |
Output is correct |
58 |
Correct |
86 ms |
11516 KB |
Output is correct |
59 |
Correct |
66 ms |
10312 KB |
Output is correct |
60 |
Correct |
48 ms |
10444 KB |
Output is correct |
61 |
Correct |
50 ms |
10664 KB |
Output is correct |
62 |
Correct |
52 ms |
10684 KB |
Output is correct |
63 |
Correct |
55 ms |
10228 KB |
Output is correct |
64 |
Correct |
53 ms |
10612 KB |
Output is correct |
65 |
Correct |
55 ms |
10704 KB |
Output is correct |
66 |
Correct |
51 ms |
10256 KB |
Output is correct |
67 |
Correct |
36 ms |
8512 KB |
Output is correct |
68 |
Correct |
43 ms |
9488 KB |
Output is correct |
69 |
Correct |
53 ms |
10708 KB |
Output is correct |
70 |
Correct |
50 ms |
10284 KB |
Output is correct |
71 |
Correct |
34 ms |
8264 KB |
Output is correct |
72 |
Correct |
40 ms |
9028 KB |
Output is correct |
73 |
Correct |
56 ms |
10744 KB |
Output is correct |
74 |
Correct |
48 ms |
10088 KB |
Output is correct |
75 |
Correct |
72 ms |
10284 KB |
Output is correct |
76 |
Correct |
66 ms |
10164 KB |
Output is correct |
77 |
Correct |
55 ms |
10324 KB |
Output is correct |
78 |
Correct |
35 ms |
7888 KB |
Output is correct |
79 |
Correct |
42 ms |
8732 KB |
Output is correct |
80 |
Correct |
70 ms |
10184 KB |
Output is correct |
81 |
Correct |
65 ms |
10508 KB |
Output is correct |
82 |
Correct |
53 ms |
10568 KB |
Output is correct |