#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 100006;
int n, maxDepth[MAX], depth[MAX], childs[MAX];
vector<int> adj[MAX], c[MAX];
int nextShort[MAX], e[MAX], resShort;
int nextLong[MAX];
long long resLong;
bool visited[MAX];
void dfs(int x, int prev = -1) {
for (auto &i: adj[x]) if (i != prev) {
c[x].push_back(i);
dfs(i, x);
}
}
void dfsShort(int x) {
e[x] = x;
vector<int> zero;
for (auto &i: c[x]) {
if (!childs[i]) zero.push_back(i), e[x] = i;
else {
dfsShort(i);
if (childs[i]) childs[x]--;
else zero.push_back(i), e[x] = i;
}
}
if (!childs[x]) return;
if (zero.empty()) return;
nextShort[x] = zero[0];
for (int i = 0; i < (int)zero.size(); i++) {
if (i != (int)zero.size() - 1) nextShort[zero[i]] = zero[i + 1];
else nextShort[zero[i]] = x;
resShort += 2;
}
}
void depthFrom(int x, int prev = -1) {
maxDepth[x] = depth[x];
for (auto &i: adj[x]) if (i != prev && !visited[i]) {
depth[i] = depth[x] + 1;
depthFrom(i, x);
maxDepth[x] = max(maxDepth[x], maxDepth[i]);
}
}
pair<int, int> farthest(int x, int prev = -1) {
for (auto &i: adj[x]) if (i != prev && !visited[i]) {
if (maxDepth[i] == maxDepth[x]) {
pair<int, int> t = farthest(i, x);
return { t.first, t.second + 1 };
}
}
return { x, 0 };
}
pair<pair<int, int>, int> d() {
depth[0] = 0;
depthFrom(0);
int t = farthest(0).first;
depth[t] = 0;
depthFrom(t);
pair<int, int> u = farthest(t);
return { make_pair(u.first, t), u.second };
}
int main() {
scanf("%d", &n);
fill(nextShort, nextShort + n, -1);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0);
for (int i = 0; i < n; i++) childs[i] = (int)c[i].size();
dfsShort(0);
if (nextShort[0] == -1) {
nextShort[0] = c[0][0];
nextShort[e[c[0][0]]] = 0;
resShort += 2;
}
if (n > 1000) {
printf("%d 0\n", resShort);
for (int i = 0; i < n; i++) printf("%d ", nextShort[i] + 1);
puts("");
for (int i = 0; i < n - 1; i++) printf("%d ", i + 2);
printf("1");
return 0;
}
for (int i = 0; i < n / 2 - n % 2; i++) {
auto it = d();
resLong += 2 * it.second;
visited[it.first.first] = visited[it.first.second] = true;
nextLong[it.first.first] = it.first.second;
nextLong[it.first.second] = it.first.first;
}
if (n % 2) {
vector<int> v;
for (int i = 0; i < n; i++) if (!visited[i]) v.push_back(i);
nextLong[v[0]] = v[1];
nextLong[v[1]] = v[2];
nextLong[v[2]] = v[0];
resLong += 4;
}
printf("%d %lld\n", resShort, resLong);
for (int i = 0; i < n; i++) printf("%d ", nextShort[i] + 1);
puts("");
for (int i = 0; i < n; i++) printf("%d ", nextLong[i] + 1);
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Village.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
77 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4972 KB |
Output is correct |
2 |
Correct |
3 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
3 ms |
4972 KB |
Output is correct |
7 |
Correct |
3 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4972 KB |
Output is correct |
10 |
Correct |
3 ms |
4972 KB |
Output is correct |
11 |
Partially correct |
3 ms |
4972 KB |
Partially correct |
12 |
Partially correct |
3 ms |
4972 KB |
Partially correct |
13 |
Correct |
3 ms |
4972 KB |
Output is correct |
14 |
Correct |
4 ms |
4972 KB |
Output is correct |
15 |
Correct |
3 ms |
4972 KB |
Output is correct |
16 |
Correct |
4 ms |
4972 KB |
Output is correct |
17 |
Correct |
3 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
5 ms |
5100 KB |
Partially correct |
2 |
Partially correct |
5 ms |
5100 KB |
Partially correct |
3 |
Partially correct |
5 ms |
5100 KB |
Partially correct |
4 |
Partially correct |
10 ms |
5100 KB |
Partially correct |
5 |
Partially correct |
14 ms |
5100 KB |
Partially correct |
6 |
Partially correct |
15 ms |
5100 KB |
Partially correct |
7 |
Partially correct |
22 ms |
5228 KB |
Partially correct |
8 |
Partially correct |
18 ms |
5100 KB |
Partially correct |
9 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
10 |
Partially correct |
17 ms |
5100 KB |
Partially correct |
11 |
Partially correct |
8 ms |
5100 KB |
Partially correct |
12 |
Correct |
11 ms |
5100 KB |
Output is correct |
13 |
Correct |
11 ms |
5100 KB |
Output is correct |
14 |
Partially correct |
11 ms |
5100 KB |
Partially correct |
15 |
Partially correct |
6 ms |
5100 KB |
Partially correct |
16 |
Correct |
13 ms |
5100 KB |
Output is correct |
17 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
18 |
Partially correct |
10 ms |
5100 KB |
Partially correct |
19 |
Partially correct |
11 ms |
5100 KB |
Partially correct |
20 |
Correct |
11 ms |
5100 KB |
Output is correct |
21 |
Partially correct |
10 ms |
5100 KB |
Partially correct |
22 |
Partially correct |
11 ms |
5100 KB |
Partially correct |
23 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
24 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
25 |
Partially correct |
6 ms |
5100 KB |
Partially correct |
26 |
Partially correct |
14 ms |
5228 KB |
Partially correct |
27 |
Partially correct |
6 ms |
5228 KB |
Partially correct |
28 |
Partially correct |
14 ms |
5228 KB |
Partially correct |
29 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
30 |
Correct |
10 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4972 KB |
Output is correct |
2 |
Correct |
3 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
3 ms |
4972 KB |
Output is correct |
7 |
Correct |
3 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4972 KB |
Output is correct |
10 |
Correct |
3 ms |
4972 KB |
Output is correct |
11 |
Partially correct |
3 ms |
4972 KB |
Partially correct |
12 |
Partially correct |
3 ms |
4972 KB |
Partially correct |
13 |
Correct |
3 ms |
4972 KB |
Output is correct |
14 |
Correct |
4 ms |
4972 KB |
Output is correct |
15 |
Correct |
3 ms |
4972 KB |
Output is correct |
16 |
Correct |
4 ms |
4972 KB |
Output is correct |
17 |
Correct |
3 ms |
4972 KB |
Output is correct |
18 |
Partially correct |
5 ms |
5100 KB |
Partially correct |
19 |
Partially correct |
5 ms |
5100 KB |
Partially correct |
20 |
Partially correct |
5 ms |
5100 KB |
Partially correct |
21 |
Partially correct |
10 ms |
5100 KB |
Partially correct |
22 |
Partially correct |
14 ms |
5100 KB |
Partially correct |
23 |
Partially correct |
15 ms |
5100 KB |
Partially correct |
24 |
Partially correct |
22 ms |
5228 KB |
Partially correct |
25 |
Partially correct |
18 ms |
5100 KB |
Partially correct |
26 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
27 |
Partially correct |
17 ms |
5100 KB |
Partially correct |
28 |
Partially correct |
8 ms |
5100 KB |
Partially correct |
29 |
Correct |
11 ms |
5100 KB |
Output is correct |
30 |
Correct |
11 ms |
5100 KB |
Output is correct |
31 |
Partially correct |
11 ms |
5100 KB |
Partially correct |
32 |
Partially correct |
6 ms |
5100 KB |
Partially correct |
33 |
Correct |
13 ms |
5100 KB |
Output is correct |
34 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
35 |
Partially correct |
10 ms |
5100 KB |
Partially correct |
36 |
Partially correct |
11 ms |
5100 KB |
Partially correct |
37 |
Correct |
11 ms |
5100 KB |
Output is correct |
38 |
Partially correct |
10 ms |
5100 KB |
Partially correct |
39 |
Partially correct |
11 ms |
5100 KB |
Partially correct |
40 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
41 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
42 |
Partially correct |
6 ms |
5100 KB |
Partially correct |
43 |
Partially correct |
14 ms |
5228 KB |
Partially correct |
44 |
Partially correct |
6 ms |
5228 KB |
Partially correct |
45 |
Partially correct |
14 ms |
5228 KB |
Partially correct |
46 |
Partially correct |
12 ms |
5100 KB |
Partially correct |
47 |
Correct |
10 ms |
5100 KB |
Output is correct |
48 |
Partially correct |
90 ms |
11628 KB |
Partially correct |
49 |
Partially correct |
99 ms |
12140 KB |
Partially correct |
50 |
Partially correct |
97 ms |
12160 KB |
Partially correct |
51 |
Partially correct |
91 ms |
10604 KB |
Partially correct |
52 |
Partially correct |
101 ms |
12140 KB |
Partially correct |
53 |
Partially correct |
92 ms |
11444 KB |
Partially correct |
54 |
Partially correct |
48 ms |
14188 KB |
Partially correct |
55 |
Partially correct |
128 ms |
24348 KB |
Partially correct |
56 |
Partially correct |
109 ms |
18028 KB |
Partially correct |
57 |
Partially correct |
122 ms |
16108 KB |
Partially correct |
58 |
Partially correct |
104 ms |
14444 KB |
Partially correct |
59 |
Partially correct |
96 ms |
12140 KB |
Partially correct |
60 |
Partially correct |
62 ms |
11108 KB |
Partially correct |
61 |
Partially correct |
71 ms |
11340 KB |
Partially correct |
62 |
Partially correct |
74 ms |
11372 KB |
Partially correct |
63 |
Partially correct |
70 ms |
11116 KB |
Partially correct |
64 |
Partially correct |
80 ms |
11756 KB |
Partially correct |
65 |
Partially correct |
69 ms |
11628 KB |
Partially correct |
66 |
Partially correct |
64 ms |
11116 KB |
Partially correct |
67 |
Partially correct |
50 ms |
9720 KB |
Partially correct |
68 |
Partially correct |
79 ms |
10604 KB |
Partially correct |
69 |
Partially correct |
70 ms |
11756 KB |
Partially correct |
70 |
Partially correct |
63 ms |
11244 KB |
Partially correct |
71 |
Partially correct |
51 ms |
9708 KB |
Partially correct |
72 |
Partially correct |
58 ms |
10220 KB |
Partially correct |
73 |
Partially correct |
68 ms |
11756 KB |
Partially correct |
74 |
Partially correct |
63 ms |
11116 KB |
Partially correct |
75 |
Partially correct |
107 ms |
12140 KB |
Partially correct |
76 |
Partially correct |
98 ms |
11500 KB |
Partially correct |
77 |
Partially correct |
79 ms |
11628 KB |
Partially correct |
78 |
Partially correct |
60 ms |
9452 KB |
Partially correct |
79 |
Partially correct |
81 ms |
10092 KB |
Partially correct |
80 |
Partially correct |
95 ms |
12140 KB |
Partially correct |
81 |
Partially correct |
76 ms |
11756 KB |
Partially correct |
82 |
Partially correct |
69 ms |
11372 KB |
Partially correct |