#include <stdio.h>
#include <stdlib.h>
#define N 500000
int *ej[N], eo[N];
void append(int **ej, int *eo, 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 main() {
static int qu[N];
static char sheep[N];
int n, k, h, i, j, cnt;
scanf("%d%d", &n, &k);
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(ej, eo, i, j), append(ej, eo, j, i);
}
for (h = 0; h < k; h++) {
scanf("%d", &i), i--;
sheep[i] = 1;
}
cnt = 0;
for (i = -1, j = 0; j < n; j++)
if (sheep[j]) {
if (i == -1)
i = j;
else if ((i + j) % 2 != 0)
qu[cnt++] = i, i = j;
else
qu[cnt++] = (i + j) / 2, i = -1;
}
if (i != -1)
qu[cnt++] = i, i = -1;
printf("%d\n", cnt);
for (h = 0; h < cnt; h++)
printf("%d ", qu[h] + 1);
printf("\n");
return 0;
}
Compilation message
pastiri.c: In function 'append':
pastiri.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
pastiri.c: In function 'main':
pastiri.c:21:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
pastiri.c:25:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
pastiri.c:29:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
28360 KB |
Output is correct |
2 |
Correct |
131 ms |
28824 KB |
Output is correct |
3 |
Correct |
138 ms |
28860 KB |
Output is correct |
4 |
Correct |
185 ms |
33332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
588 KB |
Sheep 3030 not protected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Sheep 128 not protected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
291 ms |
29356 KB |
Sheep 54 not protected |
2 |
Halted |
0 ms |
0 KB |
- |