#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <numeric>
using namespace std;
using ll = long long;;
const int MAXN = 1e5 + 123;
vector<int> edg[MAXN];
bitset<MAXN> used;
int rt[MAXN];
bool dfs_getmark(int v, bitset<MAXN>& mark) {
bool is_edge = false;
for (auto u : edg[v]) {
if (u == rt[v])
continue;
rt[u] = v;
is_edge |= dfs_getmark(u, mark);
}
if (v == 0) {
mark[v] = 1;
if (is_edge)
return true;
for (auto u : edg[v]) {
if (u == rt[v])
continue;
mark[u] = 0;
return true;
}
}
if (is_edge)
mark[v] = 1;
return !is_edge;
}
void dfs_onmark(int v, bitset<MAXN>& mark, vector<int>& ans) {
ans.push_back(v);
for (auto u : edg[v]) {
if (u == rt[v] || mark[u])
continue;
dfs_onmark(u, mark, ans);
}
}
pair<int, vector<int>> solve_min(int n) {
used = 0;
bitset<MAXN> mark;
dfs_getmark(0, mark);
vector<int> ans(n);
for (int i = 0; i < n; i = mark._Find_next(i)) {
vector<int> ccl;
dfs_onmark(i, mark, ccl);
int m = ccl.size();
for (int j = 0; j < m; ++j) {
ans[ccl[j]] = ccl[(j + 1) % m];
}
}
return {2 * (n - mark.count()), ans};
}
int depth[MAXN];
int sz[MAXN];
void init_dfs(int v) {
sz[v] = 1;
for (auto u : edg[v]) {
if (u == rt[v])
continue;
rt[u] = v;
depth[u] = depth[v] + 1;
init_dfs(u);
sz[v] += sz[u];
}
}
int find_centroid(int v) {
for (auto u : edg[v]) {
if (u == rt[v])
continue;
if (sz[u] * 2 > sz[0])
return find_centroid(u);
}
return v;
}
void dfs_tin(int v, vector<int>& ans) {
ans.push_back(v);
for (auto u : edg[v]) {
if (u == rt[v])
continue;
dfs_tin(u, ans);
}
}
pair<ll, vector<int>> solve_max(int n) {
used = 0;
for (int i = 0; i < n; ++i)
rt[i] = -1;
init_dfs(0);
int ctr = find_centroid(0);
for (int i = 0; i < n; ++i)
rt[i] = -1;
depth[ctr] = 0;
init_dfs(ctr);
vector<int> tin;
dfs_tin(ctr, tin);
vector<int> ans(n);
ll smd = 0;
//for (auto x : tin)
//cout << x + 1 << ' ';
//cout << endl;
for (int i = 0; i < n; ++i) {
ans[tin[i]] = tin[(i + n / 2) % n];
smd += depth[i];
}
return {smd * 2, ans};
}
signed main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
edg[u].push_back(v);
edg[v].push_back(u);
}
auto ansmn = solve_min(n);
auto ansmx = solve_max(n);
cout << ansmn.first << ' ' << ansmx.first << '\n';
for (auto x : ansmn.second)
cout << x + 1 << ' ';
cout << '\n';
for (auto x : ansmx.second)
cout << x + 1 << ' ';
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2660 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 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 |
2572 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2656 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2656 KB |
Output is correct |
16 |
Correct |
2 ms |
2664 KB |
Output is correct |
17 |
Correct |
2 ms |
2660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2664 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 |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2664 KB |
Output is correct |
18 |
Correct |
3 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2664 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 |
3 ms |
2644 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 |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2660 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 |
1 ms |
2660 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 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 |
2572 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2656 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2656 KB |
Output is correct |
16 |
Correct |
2 ms |
2664 KB |
Output is correct |
17 |
Correct |
2 ms |
2660 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2660 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
3 ms |
2644 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2664 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 |
3 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
3 ms |
2664 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
3 ms |
2664 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
2 ms |
2644 KB |
Output is correct |
40 |
Correct |
2 ms |
2644 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 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 |
3 ms |
2644 KB |
Output is correct |
46 |
Correct |
3 ms |
2772 KB |
Output is correct |
47 |
Correct |
3 ms |
2660 KB |
Output is correct |
48 |
Correct |
116 ms |
9808 KB |
Output is correct |
49 |
Correct |
126 ms |
10676 KB |
Output is correct |
50 |
Correct |
112 ms |
10700 KB |
Output is correct |
51 |
Correct |
90 ms |
8860 KB |
Output is correct |
52 |
Correct |
146 ms |
10520 KB |
Output is correct |
53 |
Correct |
107 ms |
9656 KB |
Output is correct |
54 |
Correct |
68 ms |
9964 KB |
Output is correct |
55 |
Correct |
191 ms |
17748 KB |
Output is correct |
56 |
Correct |
131 ms |
14020 KB |
Output is correct |
57 |
Correct |
157 ms |
12812 KB |
Output is correct |
58 |
Correct |
122 ms |
11776 KB |
Output is correct |
59 |
Correct |
112 ms |
10076 KB |
Output is correct |
60 |
Correct |
87 ms |
11248 KB |
Output is correct |
61 |
Correct |
91 ms |
10876 KB |
Output is correct |
62 |
Correct |
101 ms |
10956 KB |
Output is correct |
63 |
Correct |
90 ms |
10428 KB |
Output is correct |
64 |
Correct |
103 ms |
10848 KB |
Output is correct |
65 |
Correct |
120 ms |
10956 KB |
Output is correct |
66 |
Correct |
94 ms |
10480 KB |
Output is correct |
67 |
Correct |
69 ms |
8656 KB |
Output is correct |
68 |
Correct |
86 ms |
9704 KB |
Output is correct |
69 |
Correct |
107 ms |
10840 KB |
Output is correct |
70 |
Correct |
96 ms |
10516 KB |
Output is correct |
71 |
Correct |
61 ms |
8388 KB |
Output is correct |
72 |
Correct |
69 ms |
9196 KB |
Output is correct |
73 |
Correct |
107 ms |
11068 KB |
Output is correct |
74 |
Correct |
97 ms |
10348 KB |
Output is correct |
75 |
Correct |
137 ms |
10416 KB |
Output is correct |
76 |
Correct |
161 ms |
10360 KB |
Output is correct |
77 |
Correct |
108 ms |
10708 KB |
Output is correct |
78 |
Correct |
70 ms |
7932 KB |
Output is correct |
79 |
Correct |
78 ms |
8908 KB |
Output is correct |
80 |
Correct |
120 ms |
10464 KB |
Output is correct |
81 |
Correct |
94 ms |
10732 KB |
Output is correct |
82 |
Correct |
94 ms |
10820 KB |
Output is correct |