이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int *ej[N], eo[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 aa[N], dp[N][2][2];
void dfs(int p, int i) {
static int xx[2][2], yy[2][2];
int o, a, b;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs(i, j);
}
for (a = 0; a < 2; a++)
xx[a][0] = 0, xx[a][1] = INF;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
for (a = 0; a < 2; a++)
for (b = 0; b < 2; b++)
yy[a][b] = min(xx[a][b] + dp[j][a][0], xx[a][b ^ 1] + dp[j][a][1]);
for (a = 0; a < 2; a++)
for (b = 0; b < 2; b++)
xx[a][b] = min(yy[a][b], INF);
}
}
for (a = 0; a < 2; a++)
for (b = 0; b < 2; b++)
dp[i][aa[i] ^ a ^ b][a] = xx[a][b] + a;
}
int main() {
int n, h, i, j, x;
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);
}
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
dfs(-1, 0);
x = min(dp[0][0][0], dp[0][0][1]);
if (x == INF)
printf("impossible\n");
else
printf("%d\n", x);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.c: In function 'append':
xanadu.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
xanadu.c: In function 'main':
xanadu.c:53:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
xanadu.c:57:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
xanadu.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |