Submission #500990

#TimeUsernameProblemLanguageResultExecution timeMemory
500990rainboyWells (CEOI21_wells)C11
18 / 100
1 ms588 KiB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	200
#define INF	0x3f3f3f3f3f3f3f3fLL

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 sz[N + 1];

void dfs2(int p, int i) {
	int o, di, dj;

	dp[i][0] = ww[i], dp[i][1] = 0, 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, INF, sz[j] * sizeof *dp[i]);
			for (di = sz[i]; di >= 0; di--) {
				long long x = dp[i][di];

				dp[i][di] = INF;
				for (dj = sz[j]; dj >= 0; dj--) {
					long long y = dp[j][dj];

					if (di + dj < k) {
						int d = di == 0 ? 0 : max(di, dj + 1);

						dp[i][d] = min(dp[i][d], x + y);
					}
				}
			}
			sz[i] += sz[j];
		}
	}
}

int main() {
	int h, i, j, d;
	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);
	for (d = 0; d < n; d++)
		if (dp[0][d] == w) {
			printf("YES\n");
			printf("0\n");
			return 0;
		}
	printf("NO\n");
	printf("0\n");
	return 0;
}

Compilation message (stderr)

wells.c: In function 'append':
wells.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   16 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
wells.c: In function 'dfs2':
wells.c:6:13: warning: overflow in conversion from 'long long int' to 'int' changes value from '4557430888798830399' to '1061109567' [-Woverflow]
    6 | #define INF 0x3f3f3f3f3f3f3f3fLL
      |             ^~~~~~~~~~~~~~~~~~~~
wells.c:52:30: note: in expansion of macro 'INF'
   52 |    memset(dp[i] + sz[i] + 1, INF, sz[j] * sizeof *dp[i]);
      |                              ^~~
wells.c: In function 'main':
wells.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
wells.c:80:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |   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...