#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
const int maxn = 1e5 + 1;
vector <int> adj[maxn];
vector <int> min_path_nodes;
pair <int, bool> dfs(int s, int p, int d) {
pair <int, bool> ans = {0, false};
for (auto i : adj[s]) {
if (i == p)
continue;
auto res = dfs(i, s, d+1);
if (!res.second) {
ans.second = true;
ans.first += 2;
swap(min_path_nodes[s], min_path_nodes[i]);
}
ans.first += res.first;
}
return ans;
}
int sz[maxn];
vector <int> ord;
void dfs2(int s, int p) {
ord.push_back(s);
sz[s] = 1;
for (auto i : adj[s]) {
if (i == p)
continue;
dfs2(i, s);
sz[s] += sz[i];
}
}
int main() {
int n;
cin >> n;
vector <pair <int, int>> edges;
for (int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
edges.push_back({a, b});
}
vector <int> nums(n);
for (int i = 0; i < n; i++)
nums[i] = i;
min_path_nodes = nums;
auto res = dfs(0, 0, 0);
int min_path_len = res.first;
if (res.second == false) {
min_path_len += 2;
swap(min_path_nodes[0], min_path_nodes[adj[0][0]]);
}
// smart part - uses centroid decomposition but not in the way you think
dfs2(0, 0);
vector <int> max_path_nodes = nums;
ll max_path_len = 0;
for (int i = 0; i < n-1; i++) {
int a = edges[i].first, b = edges[i].second;
if (sz[a] > sz[b])
max_path_len += min(sz[0] - sz[b], sz[b]) * 2;
else
max_path_len += min(sz[0] - sz[a], sz[a]) * 2;
}
for (int i = 0; i < n; i++) {
int u = ord[i], v = ord[(i + n / 2) % n];
max_path_nodes[u] = v;
}
cout << min_path_len << ' ' << max_path_len << '\n';
for (auto i : min_path_nodes)
cout << i+1 << ' ';
cout << '\n';
for (int i = 0; i < n; i++)
cout << max_path_nodes[i] + 1 << ' ';
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2912 KB |
Output is correct |
2 |
Correct |
1 ms |
3160 KB |
Output is correct |
3 |
Correct |
1 ms |
3008 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2996 KB |
Output is correct |
11 |
Correct |
1 ms |
2916 KB |
Output is correct |
12 |
Correct |
1 ms |
3004 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
3000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2992 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
3004 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2920 KB |
Output is correct |
12 |
Correct |
1 ms |
3024 KB |
Output is correct |
13 |
Correct |
2 ms |
2916 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2904 KB |
Output is correct |
17 |
Correct |
2 ms |
2904 KB |
Output is correct |
18 |
Correct |
1 ms |
2904 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2904 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2912 KB |
Output is correct |
2 |
Correct |
1 ms |
3160 KB |
Output is correct |
3 |
Correct |
1 ms |
3008 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2996 KB |
Output is correct |
11 |
Correct |
1 ms |
2916 KB |
Output is correct |
12 |
Correct |
1 ms |
3004 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
3000 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2992 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
3004 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
2920 KB |
Output is correct |
29 |
Correct |
1 ms |
3024 KB |
Output is correct |
30 |
Correct |
2 ms |
2916 KB |
Output is correct |
31 |
Correct |
1 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2904 KB |
Output is correct |
33 |
Correct |
2 ms |
2904 KB |
Output is correct |
34 |
Correct |
2 ms |
2904 KB |
Output is correct |
35 |
Correct |
1 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
2908 KB |
Output is correct |
38 |
Correct |
1 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2904 KB |
Output is correct |
40 |
Correct |
1 ms |
2908 KB |
Output is correct |
41 |
Correct |
1 ms |
2908 KB |
Output is correct |
42 |
Correct |
1 ms |
2908 KB |
Output is correct |
43 |
Correct |
1 ms |
2908 KB |
Output is correct |
44 |
Correct |
1 ms |
2908 KB |
Output is correct |
45 |
Correct |
2 ms |
2908 KB |
Output is correct |
46 |
Correct |
2 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2908 KB |
Output is correct |
48 |
Correct |
74 ms |
10468 KB |
Output is correct |
49 |
Correct |
73 ms |
11016 KB |
Output is correct |
50 |
Correct |
70 ms |
11216 KB |
Output is correct |
51 |
Correct |
59 ms |
9448 KB |
Output is correct |
52 |
Correct |
70 ms |
11060 KB |
Output is correct |
53 |
Correct |
63 ms |
10336 KB |
Output is correct |
54 |
Correct |
40 ms |
10596 KB |
Output is correct |
55 |
Correct |
83 ms |
18628 KB |
Output is correct |
56 |
Correct |
75 ms |
14480 KB |
Output is correct |
57 |
Correct |
81 ms |
13440 KB |
Output is correct |
58 |
Correct |
73 ms |
12232 KB |
Output is correct |
59 |
Correct |
75 ms |
11204 KB |
Output is correct |
60 |
Correct |
66 ms |
11200 KB |
Output is correct |
61 |
Correct |
68 ms |
11712 KB |
Output is correct |
62 |
Correct |
67 ms |
11472 KB |
Output is correct |
63 |
Correct |
61 ms |
10948 KB |
Output is correct |
64 |
Correct |
67 ms |
11464 KB |
Output is correct |
65 |
Correct |
66 ms |
11560 KB |
Output is correct |
66 |
Correct |
60 ms |
10956 KB |
Output is correct |
67 |
Correct |
47 ms |
9412 KB |
Output is correct |
68 |
Correct |
55 ms |
10180 KB |
Output is correct |
69 |
Correct |
68 ms |
11464 KB |
Output is correct |
70 |
Correct |
60 ms |
10948 KB |
Output is correct |
71 |
Correct |
47 ms |
9156 KB |
Output is correct |
72 |
Correct |
51 ms |
9928 KB |
Output is correct |
73 |
Correct |
67 ms |
11460 KB |
Output is correct |
74 |
Correct |
59 ms |
10964 KB |
Output is correct |
75 |
Correct |
69 ms |
10968 KB |
Output is correct |
76 |
Correct |
69 ms |
10952 KB |
Output is correct |
77 |
Correct |
63 ms |
11164 KB |
Output is correct |
78 |
Correct |
51 ms |
8644 KB |
Output is correct |
79 |
Correct |
56 ms |
9412 KB |
Output is correct |
80 |
Correct |
73 ms |
11016 KB |
Output is correct |
81 |
Correct |
71 ms |
11384 KB |
Output is correct |
82 |
Correct |
62 ms |
11208 KB |
Output is correct |