Submission #482871

#TimeUsernameProblemLanguageResultExecution timeMemory
482871rainboySvjetlo (COCI20_svjetlo)C11
110 / 110
592 ms113360 KiB
#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) { if (!empty[j]) { for (k = 0; k <= 2; k++) for (e = 0; e <= 2; e++) for (c = 0; c < 2; c++) dr[k][e][c] = INF; j_ = j_ == -1 ? j : -2; } else { for (k = 0; k <= 2; k++) for (e = 0; e <= 2; e++) for (c = 0; c < 2; c++) dr[k][e][c] = dq[k][e][c]; } 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", dp[0][2][0]); return 0; }

Compilation message (stderr)

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:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |  scanf("%d%s", &n, cc);
      |  ^~~~~~~~~~~~~~~~~~~~~
svjetlo.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...