#include <iostream>
#include <vector>
using i64 = int64_t;
const i64 INFINITY = __INT64_MAX__ / 1000;
std::pair<i64, i64> dfs(std::vector<std::vector<i64>> const &neighbours,
std::vector<std::pair<i64, i64>> &ann, i64 node,
i64 parent) {
auto child_count = neighbours[node].size() - (parent != -1);
if (child_count == 0) {
return ann[node] = {INFINITY, 2};
} else {
std::vector<std::pair<i64, i64>> results;
for (auto neighbour : neighbours[node]) {
if (neighbour == parent) continue;
results.push_back(dfs(neighbours, ann, neighbour, node));
}
// for k we have to move the root at least once
i64 best_choice = -1;
i64 why = INFINITY;
for (i64 i = 0; i < child_count; i++) {
if (results[i].second - results[i].first < why) {
best_choice = i;
why = results[i].second - results[i].first;
}
}
i64 k = 0;
for (i64 i = 0; i < child_count; i++) {
if (i == best_choice) {
k += results[i].second;
} else {
k += std::min(results[i].first, results[i].second);
}
}
i64 l = 2;
for (i64 i = 0; i < child_count; i++) {
l += std::min(results[i].first, results[i].second);
}
return ann[node] = {k, l};
}
}
std::pair<i64, i64> dfs_constructive(
std::vector<std::vector<i64>> const &neighbours,
std::vector<std::pair<i64, i64>> const &ann, i64 node, i64 parent, i64 mode,
std::vector<i64> &assignment) {
auto child_count = neighbours[node].size() - (parent != -1);
if (child_count == 0) {
if (mode & 0b01) {
return {node, node};
} else {
std::cerr << "SOMETHING IS WRONG\n";
return {-1, -1};
}
} else {
std::vector<i64> children;
for (auto neighbour : neighbours[node]) {
if (neighbour == parent) continue;
children.push_back(neighbour);
}
if (mode & 0b10 ||
(mode == 0b11 && ann[node].first <= ann[node].second)) {
// for k we have to move the root at least once
i64 best_choice = -1;
i64 why = INFINITY;
for (i64 i = 0; i < child_count; i++) {
if (ann[children[i]].second - ann[children[i]].first < why) {
best_choice = i;
why = ann[children[i]].second - ann[children[i]].first;
}
}
i64 moving = node;
for (i64 i = 0; i < child_count; i++) {
auto [to, from] = dfs_constructive(
neighbours, ann, children[i], node,
i == best_choice ? 0b01 : 0b11, assignment);
if (to != -1) {
assignment[moving] = to;
moving = from;
}
}
assignment[moving] = node; // ig
return {-1, -1};
} else {
i64 moving = node;
for (auto child : children) {
auto [to, from] = dfs_constructive(neighbours, ann, child, node,
0b11, assignment);
if (to != -1) {
assignment[moving] = to;
moving = from;
}
}
return {node, moving};
}
}
}
int main() {
i64 n;
std::cin >> n;
std::vector<std::vector<i64>> neighbours(n);
for (i64 i = 0; i < n - 1; i++) {
i64 a, b;
std::cin >> a >> b;
a--, b--;
neighbours[a].push_back(b);
neighbours[b].push_back(a);
}
std::vector<std::pair<i64, i64>> ann(n);
auto [k, l] = dfs(neighbours, ann, 0, -1);
std::cout << k << " 1\n";
std::vector<i64> assignment(n);
dfs_constructive(neighbours, ann, 0, -1, 0b10, assignment);
for (i64 x : assignment) {
std::cout << x + 1 << ' ';
}
std::cout << "\n";
for (i64 x : assignment) {
std::cout << x + 1 << ' ';
}
}
Compilation message
Village.cpp: In function 'std::pair<long int, long int> dfs(const std::vector<std::vector<long int> >&, std::vector<std::pair<long int, long int> >&, i64, i64)':
Village.cpp:24:27: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
24 | for (i64 i = 0; i < child_count; i++) {
| ~~^~~~~~~~~~~~~
Village.cpp:32:27: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
32 | for (i64 i = 0; i < child_count; i++) {
| ~~^~~~~~~~~~~~~
Village.cpp:41:27: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
41 | for (i64 i = 0; i < child_count; i++) {
| ~~^~~~~~~~~~~~~
Village.cpp: In function 'std::pair<long int, long int> dfs_constructive(const std::vector<std::vector<long int> >&, const std::vector<std::pair<long int, long int> >&, i64, i64, i64, std::vector<long int>&)':
Village.cpp:73:31: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
73 | for (i64 i = 0; i < child_count; i++) {
| ~~^~~~~~~~~~~~~
Village.cpp:82:31: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
82 | for (i64 i = 0; i < child_count; i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
212 KB |
Partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Partially correct |
4 |
Partially correct |
0 ms |
212 KB |
Partially correct |
5 |
Partially correct |
0 ms |
212 KB |
Partially correct |
6 |
Partially correct |
1 ms |
212 KB |
Partially correct |
7 |
Partially correct |
0 ms |
212 KB |
Partially correct |
8 |
Partially correct |
0 ms |
212 KB |
Partially correct |
9 |
Partially correct |
1 ms |
212 KB |
Partially correct |
10 |
Partially correct |
0 ms |
212 KB |
Partially correct |
11 |
Partially correct |
1 ms |
212 KB |
Partially correct |
12 |
Partially correct |
1 ms |
212 KB |
Partially correct |
13 |
Partially correct |
1 ms |
304 KB |
Partially correct |
14 |
Partially correct |
1 ms |
212 KB |
Partially correct |
15 |
Partially correct |
1 ms |
212 KB |
Partially correct |
16 |
Partially correct |
1 ms |
212 KB |
Partially correct |
17 |
Partially correct |
1 ms |
212 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
296 KB |
Partially correct |
2 |
Partially correct |
1 ms |
340 KB |
Partially correct |
3 |
Partially correct |
1 ms |
340 KB |
Partially correct |
4 |
Partially correct |
1 ms |
304 KB |
Partially correct |
5 |
Partially correct |
1 ms |
340 KB |
Partially correct |
6 |
Partially correct |
1 ms |
340 KB |
Partially correct |
7 |
Partially correct |
1 ms |
468 KB |
Partially correct |
8 |
Partially correct |
1 ms |
340 KB |
Partially correct |
9 |
Partially correct |
1 ms |
340 KB |
Partially correct |
10 |
Partially correct |
1 ms |
340 KB |
Partially correct |
11 |
Partially correct |
1 ms |
340 KB |
Partially correct |
12 |
Partially correct |
1 ms |
304 KB |
Partially correct |
13 |
Partially correct |
1 ms |
340 KB |
Partially correct |
14 |
Partially correct |
1 ms |
340 KB |
Partially correct |
15 |
Partially correct |
1 ms |
340 KB |
Partially correct |
16 |
Partially correct |
1 ms |
340 KB |
Partially correct |
17 |
Partially correct |
1 ms |
340 KB |
Partially correct |
18 |
Partially correct |
1 ms |
340 KB |
Partially correct |
19 |
Partially correct |
1 ms |
304 KB |
Partially correct |
20 |
Partially correct |
1 ms |
300 KB |
Partially correct |
21 |
Partially correct |
1 ms |
340 KB |
Partially correct |
22 |
Partially correct |
1 ms |
312 KB |
Partially correct |
23 |
Partially correct |
2 ms |
340 KB |
Partially correct |
24 |
Partially correct |
1 ms |
340 KB |
Partially correct |
25 |
Partially correct |
1 ms |
304 KB |
Partially correct |
26 |
Partially correct |
2 ms |
308 KB |
Partially correct |
27 |
Partially correct |
1 ms |
340 KB |
Partially correct |
28 |
Partially correct |
1 ms |
340 KB |
Partially correct |
29 |
Partially correct |
1 ms |
340 KB |
Partially correct |
30 |
Partially correct |
1 ms |
340 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
212 KB |
Partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Partially correct |
4 |
Partially correct |
0 ms |
212 KB |
Partially correct |
5 |
Partially correct |
0 ms |
212 KB |
Partially correct |
6 |
Partially correct |
1 ms |
212 KB |
Partially correct |
7 |
Partially correct |
0 ms |
212 KB |
Partially correct |
8 |
Partially correct |
0 ms |
212 KB |
Partially correct |
9 |
Partially correct |
1 ms |
212 KB |
Partially correct |
10 |
Partially correct |
0 ms |
212 KB |
Partially correct |
11 |
Partially correct |
1 ms |
212 KB |
Partially correct |
12 |
Partially correct |
1 ms |
212 KB |
Partially correct |
13 |
Partially correct |
1 ms |
304 KB |
Partially correct |
14 |
Partially correct |
1 ms |
212 KB |
Partially correct |
15 |
Partially correct |
1 ms |
212 KB |
Partially correct |
16 |
Partially correct |
1 ms |
212 KB |
Partially correct |
17 |
Partially correct |
1 ms |
212 KB |
Partially correct |
18 |
Partially correct |
1 ms |
296 KB |
Partially correct |
19 |
Partially correct |
1 ms |
340 KB |
Partially correct |
20 |
Partially correct |
1 ms |
340 KB |
Partially correct |
21 |
Partially correct |
1 ms |
304 KB |
Partially correct |
22 |
Partially correct |
1 ms |
340 KB |
Partially correct |
23 |
Partially correct |
1 ms |
340 KB |
Partially correct |
24 |
Partially correct |
1 ms |
468 KB |
Partially correct |
25 |
Partially correct |
1 ms |
340 KB |
Partially correct |
26 |
Partially correct |
1 ms |
340 KB |
Partially correct |
27 |
Partially correct |
1 ms |
340 KB |
Partially correct |
28 |
Partially correct |
1 ms |
340 KB |
Partially correct |
29 |
Partially correct |
1 ms |
304 KB |
Partially correct |
30 |
Partially correct |
1 ms |
340 KB |
Partially correct |
31 |
Partially correct |
1 ms |
340 KB |
Partially correct |
32 |
Partially correct |
1 ms |
340 KB |
Partially correct |
33 |
Partially correct |
1 ms |
340 KB |
Partially correct |
34 |
Partially correct |
1 ms |
340 KB |
Partially correct |
35 |
Partially correct |
1 ms |
340 KB |
Partially correct |
36 |
Partially correct |
1 ms |
304 KB |
Partially correct |
37 |
Partially correct |
1 ms |
300 KB |
Partially correct |
38 |
Partially correct |
1 ms |
340 KB |
Partially correct |
39 |
Partially correct |
1 ms |
312 KB |
Partially correct |
40 |
Partially correct |
2 ms |
340 KB |
Partially correct |
41 |
Partially correct |
1 ms |
340 KB |
Partially correct |
42 |
Partially correct |
1 ms |
304 KB |
Partially correct |
43 |
Partially correct |
2 ms |
308 KB |
Partially correct |
44 |
Partially correct |
1 ms |
340 KB |
Partially correct |
45 |
Partially correct |
1 ms |
340 KB |
Partially correct |
46 |
Partially correct |
1 ms |
340 KB |
Partially correct |
47 |
Partially correct |
1 ms |
340 KB |
Partially correct |
48 |
Partially correct |
67 ms |
10080 KB |
Partially correct |
49 |
Partially correct |
75 ms |
11092 KB |
Partially correct |
50 |
Partially correct |
82 ms |
11036 KB |
Partially correct |
51 |
Partially correct |
70 ms |
8680 KB |
Partially correct |
52 |
Partially correct |
73 ms |
10956 KB |
Partially correct |
53 |
Partially correct |
80 ms |
9932 KB |
Partially correct |
54 |
Partially correct |
41 ms |
13772 KB |
Partially correct |
55 |
Partially correct |
96 ms |
28540 KB |
Partially correct |
56 |
Partially correct |
90 ms |
19416 KB |
Partially correct |
57 |
Partially correct |
85 ms |
16588 KB |
Partially correct |
58 |
Partially correct |
84 ms |
13972 KB |
Partially correct |
59 |
Partially correct |
80 ms |
11508 KB |
Partially correct |
60 |
Partially correct |
61 ms |
12852 KB |
Partially correct |
61 |
Partially correct |
64 ms |
11424 KB |
Partially correct |
62 |
Partially correct |
66 ms |
11464 KB |
Partially correct |
63 |
Partially correct |
59 ms |
10828 KB |
Partially correct |
64 |
Partially correct |
68 ms |
11448 KB |
Partially correct |
65 |
Partially correct |
63 ms |
11636 KB |
Partially correct |
66 |
Partially correct |
58 ms |
10924 KB |
Partially correct |
67 |
Partially correct |
47 ms |
8932 KB |
Partially correct |
68 |
Partially correct |
53 ms |
9804 KB |
Partially correct |
69 |
Partially correct |
73 ms |
11692 KB |
Partially correct |
70 |
Partially correct |
58 ms |
10956 KB |
Partially correct |
71 |
Partially correct |
42 ms |
8092 KB |
Partially correct |
72 |
Partially correct |
48 ms |
9236 KB |
Partially correct |
73 |
Partially correct |
65 ms |
11620 KB |
Partially correct |
74 |
Partially correct |
61 ms |
10636 KB |
Partially correct |
75 |
Partially correct |
74 ms |
11524 KB |
Partially correct |
76 |
Partially correct |
78 ms |
10916 KB |
Partially correct |
77 |
Partially correct |
66 ms |
11060 KB |
Partially correct |
78 |
Partially correct |
45 ms |
7628 KB |
Partially correct |
79 |
Partially correct |
59 ms |
8708 KB |
Partially correct |
80 |
Partially correct |
78 ms |
11508 KB |
Partially correct |
81 |
Partially correct |
66 ms |
11320 KB |
Partially correct |
82 |
Partially correct |
70 ms |
11160 KB |
Partially correct |