Submission #500992

#TimeUsernameProblemLanguageResultExecution timeMemory
500992rainboyWells (CEOI21_wells)C11
50 / 100
2588 ms503640 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 10000 #define INF 0x3f3f3f3f3f3f3f3fLL #define MD 1000000007 long long min(long long a, long long b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int *ej[N], eo[N], n, k; 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; } long long ww[N]; int dfs1(int p, int i, int d) { int o, w; if (--d == 0) { ww[i]++; return 1; } w = 0; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) w += dfs1(i, j, d); } ww[i] += w; return w; } long long dp[N][N + 1]; int dq[N][N + 1], sz[N + 1]; void dfs2(int p, int i) { int o, di, dj; dp[i][0] = ww[i], dq[i][0] = 1, dp[i][1] = 0, dq[i][1] = 1, sz[i] = 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { dfs2(i, j); memset(dp[i] + sz[i] + 1, 0x3f, sz[j] * sizeof *dp[i]); memset(dq[i] + sz[i] + 1, 0, sz[j] * sizeof *dq[i]); for (di = sz[i]; di >= 0; di--) { long long pi = dp[i][di]; int qi = dq[i][di]; dp[i][di] = INF, dq[i][di] = 0; for (dj = sz[j]; dj >= 0; dj--) { long long pj = dp[j][dj]; int qj = dq[j][dj]; if (di + dj < k) { int d = di == 0 ? 0 : max(di, dj + 1); long long p = pi + pj; int q = (long long) qi * qj % MD; if (dp[i][d] > p) dp[i][d] = p, dq[i][d] = 0; if (dp[i][d] == p) dq[i][d] = (dq[i][d] + q) % MD; } } } sz[i] += sz[j]; } } } int main() { int h, i, j, d, yes, q; long long w; 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(i, j), append(j, i); } for (i = 0; i < n; i++) dfs1(-1, i, k); w = 0; for (i = 0; i < n; i++) w += ww[i] /= 2; w /= k; dfs2(-1, 0); yes = 0, q = 0; for (d = 0; d < n; d++) if (dp[0][d] == w) yes = 1, q = (q + dq[0][d]) % MD; printf(yes ? "YES\n" : "NO\n"); printf("%d\n", q); return 0; }

Compilation message (stderr)

wells.c: In function 'append':
wells.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   17 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
wells.c: In function 'main':
wells.c:85:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
wells.c:89:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |   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...