#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int N, dp[100005][2], ans[100005], conn[100005], seen[100005]; // 0 = isolated, 1 = attached
vector<int> graph[100005], g2[100005];
vector<int> broken[100005], unbroken[100005];
vector<int> order;
void dfs(int x, int p) {
dp[x][1] = 0;
int mindiff = 2069696969;
for (int v : graph[x]) {
if (v != p) {
dfs(v, x);
int df = max(0, dp[v][0] + 1 - dp[v][1]);
if (mindiff > df) {
mindiff = df;
conn[x] = v;
}
if (dp[v][0] + 1 > dp[v][1]) {
broken[x].push_back(v);
dp[x][1] += dp[v][0] + 1;
} else {
unbroken[x].push_back(v);
dp[x][1] += dp[v][1];
}
}
}
dp[x][0] = dp[x][1] - mindiff;
//printf("dp[%d][0]: %d\ndp[%d][1]: %d\n", x, dp[x][0], x, dp[x][1]);
}
void dfs2(int x, int state) {
int keep = -1;
if (state == 0) {
keep = conn[x];
}
if (keep != -1) {
g2[x].push_back(keep);
g2[keep].push_back(x);
//printf("edge %d - %d added\n", x, keep);
dfs2(keep, 1);
}
for (int v : broken[x]) {
if (v != keep) {
dfs2(v, 0);
}
}
for (int v : unbroken[x]) {
if (v != keep) {
g2[x].push_back(v);
g2[v].push_back(x);
//printf("edge %d - %d added\n", x, v);
dfs2(v, 1);
}
}
}
void dfs3(int x, int p) {
seen[x] = 1;
order.push_back(x);
for (int v : g2[x]) if (v != p) dfs3(v, x);
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N-1; i++) {
int a, b;
scanf("%d %d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1, 1);
dfs2(1, 0); // Create g2
for (int i = 1; i <= N; i++) {
if (!seen[i]) {
order.clear();
dfs3(i, i);
for (int j = 0; j < order.size(); j++) {
ans[order[j]] = order[(j+1)%order.size()];
}
}
}
printf("%d 69\n", 2 * (N-1-dp[1][0]));
for (int i = 1; i <= N; i++) {
if (i == 1) printf("%d", ans[i]);
else printf(" %d", ans[i]);
} printf("\n");
for (int i = 1; i <= N; i++) {
if (i == 1) printf("%d", ans[i]);
else printf(" %d", ans[i]);
} printf("\n");
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int j = 0; j < order.size(); j++) {
| ~~^~~~~~~~~~~~~~
Village.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
Village.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
2 |
Partially correct |
6 ms |
9728 KB |
Partially correct |
3 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
4 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
5 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
6 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
7 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
8 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
9 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
10 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
11 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
12 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
13 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
14 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
15 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
16 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
17 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
8 ms |
9728 KB |
Partially correct |
2 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
3 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
4 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
5 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
6 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
7 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
8 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
9 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
10 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
11 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
12 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
13 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
14 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
15 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
16 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
17 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
18 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
19 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
20 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
21 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
22 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
23 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
24 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
25 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
26 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
27 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
28 |
Partially correct |
8 ms |
9984 KB |
Partially correct |
29 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
30 |
Partially correct |
9 ms |
9856 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
2 |
Partially correct |
6 ms |
9728 KB |
Partially correct |
3 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
4 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
5 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
6 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
7 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
8 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
9 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
10 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
11 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
12 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
13 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
14 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
15 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
16 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
17 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
18 |
Partially correct |
8 ms |
9728 KB |
Partially correct |
19 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
20 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
21 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
22 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
23 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
24 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
25 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
26 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
27 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
28 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
29 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
30 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
31 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
32 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
33 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
34 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
35 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
36 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
37 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
38 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
39 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
40 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
41 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
42 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
43 |
Partially correct |
8 ms |
9856 KB |
Partially correct |
44 |
Partially correct |
7 ms |
9728 KB |
Partially correct |
45 |
Partially correct |
8 ms |
9984 KB |
Partially correct |
46 |
Partially correct |
7 ms |
9856 KB |
Partially correct |
47 |
Partially correct |
9 ms |
9856 KB |
Partially correct |
48 |
Partially correct |
130 ms |
21112 KB |
Partially correct |
49 |
Partially correct |
143 ms |
22268 KB |
Partially correct |
50 |
Partially correct |
152 ms |
22264 KB |
Partially correct |
51 |
Partially correct |
127 ms |
19580 KB |
Partially correct |
52 |
Partially correct |
163 ms |
22136 KB |
Partially correct |
53 |
Partially correct |
136 ms |
20984 KB |
Partially correct |
54 |
Partially correct |
68 ms |
21496 KB |
Partially correct |
55 |
Partially correct |
159 ms |
34040 KB |
Partially correct |
56 |
Partially correct |
161 ms |
27768 KB |
Partially correct |
57 |
Partially correct |
164 ms |
26104 KB |
Partially correct |
58 |
Partially correct |
162 ms |
24568 KB |
Partially correct |
59 |
Partially correct |
147 ms |
22136 KB |
Partially correct |
60 |
Partially correct |
99 ms |
21488 KB |
Partially correct |
61 |
Partially correct |
111 ms |
21368 KB |
Partially correct |
62 |
Partially correct |
122 ms |
21624 KB |
Partially correct |
63 |
Partially correct |
108 ms |
20984 KB |
Partially correct |
64 |
Partially correct |
130 ms |
21880 KB |
Partially correct |
65 |
Partially correct |
117 ms |
22008 KB |
Partially correct |
66 |
Partially correct |
111 ms |
21112 KB |
Partially correct |
67 |
Partially correct |
80 ms |
18424 KB |
Partially correct |
68 |
Partially correct |
108 ms |
20088 KB |
Partially correct |
69 |
Partially correct |
110 ms |
22008 KB |
Partially correct |
70 |
Partially correct |
104 ms |
21240 KB |
Partially correct |
71 |
Partially correct |
74 ms |
18296 KB |
Partially correct |
72 |
Partially correct |
88 ms |
19320 KB |
Partially correct |
73 |
Partially correct |
112 ms |
22008 KB |
Partially correct |
74 |
Partially correct |
103 ms |
20984 KB |
Partially correct |
75 |
Partially correct |
152 ms |
21880 KB |
Partially correct |
76 |
Partially correct |
145 ms |
21228 KB |
Partially correct |
77 |
Partially correct |
124 ms |
21496 KB |
Partially correct |
78 |
Partially correct |
83 ms |
17528 KB |
Partially correct |
79 |
Partially correct |
98 ms |
18808 KB |
Partially correct |
80 |
Partially correct |
148 ms |
21940 KB |
Partially correct |
81 |
Partially correct |
126 ms |
21756 KB |
Partially correct |
82 |
Partially correct |
112 ms |
21368 KB |
Partially correct |