#include <stdio.h>
#include <stdlib.h>
#define N 100000
int *ej[N], eo[N], n;
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int jj[N], pp[N], dd[N], mn;
void match(int i) {
while (dd[i] == 1) {
int j = jj[i], tmp;
if (pp[i] == i || dd[j] == 1)
tmp = pp[i], pp[i] = pp[j], pp[j] = tmp;
else
mn -= 2;
jj[i] ^= j, dd[i] = 0, jj[j] ^= i, dd[j]--;
i = j;
}
}
int sz[N], c;
void dfs1(int p, int i) {
int o, centroid;
sz[i] = 1, centroid = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j);
if (sz[j] * 2 > n)
centroid = 0;
sz[i] += sz[j];
}
}
if ((n - sz[i]) * 2 > n)
centroid = 0;
if (centroid)
c = i;
}
int qu[N], cnt; long long mx;
void dfs2(int p, int i, int d) {
int o;
mx += d * 2;
qu[cnt++] = i;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(i, j, d + 1);
}
}
int main() {
static int qq[N];
int h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
jj[i] ^= j, dd[i]++, jj[j] ^= i, dd[j]++;
}
for (i = 0; i < n; i++)
pp[i] = i;
mn = (n - 1) * 2;
for (i = 0; i < n; i++)
match(i);
dfs1(-1, 0);
dfs2(-1, c, 0);
for (i = 0; i <= (n - 1) / 2; i++)
qq[qu[i]] = qu[n / 2 + i], qq[qu[n / 2 + i]] = qu[i];
if (n % 2 == 1)
qq[qu[n - 1]] = qu[0];
printf("%d %lld\n", mn, mx);
for (i = 0; i < n; i++)
printf("%d ", pp[i] + 1);
printf("\n");
for (i = 0; i < n; i++)
printf("%d ", qq[i] + 1);
printf("\n");
return 0;
}
Compilation message
Village.c: In function 'append':
Village.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Village.c: In function 'main':
Village.c:72:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Village.c:76:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
2 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
424 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
296 KB |
Output is correct |
24 |
Correct |
1 ms |
292 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
356 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
2 ms |
432 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
424 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
300 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
296 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
296 KB |
Output is correct |
41 |
Correct |
1 ms |
292 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
356 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
312 KB |
Output is correct |
48 |
Correct |
88 ms |
8704 KB |
Output is correct |
49 |
Correct |
92 ms |
9596 KB |
Output is correct |
50 |
Correct |
96 ms |
9608 KB |
Output is correct |
51 |
Correct |
62 ms |
7432 KB |
Output is correct |
52 |
Correct |
95 ms |
9464 KB |
Output is correct |
53 |
Correct |
77 ms |
8524 KB |
Output is correct |
54 |
Correct |
39 ms |
8272 KB |
Output is correct |
55 |
Correct |
110 ms |
16748 KB |
Output is correct |
56 |
Correct |
74 ms |
12788 KB |
Output is correct |
57 |
Correct |
97 ms |
11580 KB |
Output is correct |
58 |
Correct |
96 ms |
10472 KB |
Output is correct |
59 |
Correct |
87 ms |
9572 KB |
Output is correct |
60 |
Correct |
80 ms |
9612 KB |
Output is correct |
61 |
Correct |
80 ms |
9768 KB |
Output is correct |
62 |
Correct |
73 ms |
9892 KB |
Output is correct |
63 |
Correct |
66 ms |
9260 KB |
Output is correct |
64 |
Correct |
83 ms |
9852 KB |
Output is correct |
65 |
Correct |
74 ms |
9828 KB |
Output is correct |
66 |
Correct |
62 ms |
9236 KB |
Output is correct |
67 |
Correct |
40 ms |
7292 KB |
Output is correct |
68 |
Correct |
55 ms |
8396 KB |
Output is correct |
69 |
Correct |
67 ms |
9848 KB |
Output is correct |
70 |
Correct |
64 ms |
9360 KB |
Output is correct |
71 |
Correct |
50 ms |
6908 KB |
Output is correct |
72 |
Correct |
59 ms |
7840 KB |
Output is correct |
73 |
Correct |
58 ms |
9868 KB |
Output is correct |
74 |
Correct |
74 ms |
9068 KB |
Output is correct |
75 |
Correct |
96 ms |
9200 KB |
Output is correct |
76 |
Correct |
81 ms |
9196 KB |
Output is correct |
77 |
Correct |
95 ms |
9544 KB |
Output is correct |
78 |
Correct |
51 ms |
6700 KB |
Output is correct |
79 |
Correct |
60 ms |
7624 KB |
Output is correct |
80 |
Correct |
89 ms |
9140 KB |
Output is correct |
81 |
Correct |
83 ms |
9600 KB |
Output is correct |
82 |
Correct |
73 ms |
9652 KB |
Output is correct |