#include <stdio.h>
#include <stdlib.h>
#define N 500000
#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;
}
char cc[N + 1], empty[N];
int dp[N][3][3];
void dfs(int p, int i) {
static int dq[3][3][2], dr[3][3][2];
int o, k, e, c, k1, e1, c1, k2, e2, j_;
empty[i] = cc[i] == '1';
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
if (!empty[j])
empty[i] = 0;
}
}
for (k = 0; k <= 2; k++)
for (e = 0; e <= 2; e++)
for (c = 0; c < 2; c++)
dq[k][e][c] = INF;
c = cc[i] - '0';
dq[0][0][c] = 0, dq[0][1][c ^ 1] = 2, dq[0][2][c] = 4;
dq[1][0][c ^ 1] = 1, dq[1][1][c] = 3, dq[1][2][c ^ 1] = 5;
dq[2][0][c ^ 1] = 1, dq[2][1][c] = 3, dq[2][2][c ^ 1] = 5;
j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
for (k = 0; k <= 2; k++)
for (e = 0; e <= 2; e++)
for (c = 0; c < 2; c++)
dr[k][e][c] = INF;
if (!empty[j])
j_ = j_ == -1 ? j : -2;
for (k1 = 0; k1 <= 2; k1++)
for (e1 = 0; e1 <= 2; e1++)
for (c1 = 0; c1 < 2; c1++)
for (k2 = 0; k1 + k2 <= 2; k2++)
for (e2 = 0; e2 <= 2; e2++) {
int x = dq[k1][e1][c1] + dp[j][k2][e2];
if (k2 != 1 && e2 == 0)
continue;
k = k1 + k2, e = e1, c = c1 ^ (e2 & 1);
if (k1 == 0 && k2 == 1)
x++, c ^= 1;
dr[k][e][c] = min(dr[k][e][c], x);
}
for (k = 0; k <= 2; k++)
for (e = 0; e <= 2; e++)
for (c = 0; c < 2; c++)
dq[k][e][c] = dr[k][e][c];
}
}
c = cc[i] - '0';
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && (j_ == -1 || j == j_))
dq[0][0][c] = min(dq[0][0][c], dp[j][0][0]), dq[2][0][c] = min(dq[2][0][c], dp[j][2][0]);
}
c = 1;
for (k = 0; k <= 2; k++)
for (e = 0; e <= 2; e++)
dp[i][k][e] = dq[k][e][c];
}
int main() {
int n, h, i, j;
scanf("%d%s", &n, cc);
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);
}
dfs(-1, 0);
printf("%d\n", min(dp[0][2][0], dp[0][2][0]));
return 0;
}
Compilation message
svjetlo.c: In function 'append':
svjetlo.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
svjetlo.c: In function 'main':
svjetlo.c:91:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
svjetlo.c:95:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
373 ms |
64608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
349 ms |
37152 KB |
Output is correct |
2 |
Correct |
431 ms |
46364 KB |
Output is correct |
3 |
Correct |
537 ms |
47144 KB |
Output is correct |
4 |
Correct |
415 ms |
39636 KB |
Output is correct |
5 |
Correct |
414 ms |
45988 KB |
Output is correct |
6 |
Correct |
384 ms |
40436 KB |
Output is correct |
7 |
Correct |
455 ms |
46208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |