Submission #500991

# Submission time Handle Problem Language Result Execution time Memory
500991 2022-01-01T22:09:13 Z rainboy Wells (CEOI21_wells) C
30 / 100
1 ms 716 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	200
#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

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 time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 0 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 716 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 1 ms 716 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 716 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 648 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 1 ms 716 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 1 ms 716 KB Output is correct
36 Correct 1 ms 716 KB Output is correct
37 Correct 1 ms 716 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 716 KB Output is correct
40 Correct 1 ms 716 KB Output is correct
41 Correct 1 ms 716 KB Output is correct
42 Correct 1 ms 716 KB Output is correct
43 Correct 1 ms 716 KB Output is correct
44 Correct 1 ms 716 KB Output is correct
45 Correct 1 ms 716 KB Output is correct
46 Correct 1 ms 716 KB Output is correct
47 Correct 1 ms 716 KB Output is correct
48 Correct 1 ms 716 KB Output is correct
49 Correct 1 ms 716 KB Output is correct
50 Correct 1 ms 716 KB Output is correct
51 Correct 1 ms 716 KB Output is correct
52 Correct 1 ms 716 KB Output is correct
53 Correct 1 ms 716 KB Output is correct
54 Correct 1 ms 716 KB Output is correct
55 Correct 1 ms 716 KB Output is correct
56 Correct 1 ms 716 KB Output is correct
57 Correct 1 ms 716 KB Output is correct
58 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 0 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 716 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 1 ms 716 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 716 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 648 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 1 ms 716 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 1 ms 716 KB Output is correct
36 Correct 1 ms 716 KB Output is correct
37 Correct 1 ms 716 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 716 KB Output is correct
40 Correct 1 ms 716 KB Output is correct
41 Correct 1 ms 716 KB Output is correct
42 Correct 1 ms 716 KB Output is correct
43 Correct 1 ms 716 KB Output is correct
44 Correct 1 ms 716 KB Output is correct
45 Correct 1 ms 716 KB Output is correct
46 Correct 1 ms 716 KB Output is correct
47 Correct 1 ms 716 KB Output is correct
48 Correct 1 ms 716 KB Output is correct
49 Correct 1 ms 716 KB Output is correct
50 Correct 1 ms 716 KB Output is correct
51 Correct 1 ms 716 KB Output is correct
52 Correct 1 ms 716 KB Output is correct
53 Correct 1 ms 716 KB Output is correct
54 Correct 1 ms 716 KB Output is correct
55 Correct 1 ms 716 KB Output is correct
56 Correct 1 ms 716 KB Output is correct
57 Correct 1 ms 716 KB Output is correct
58 Correct 1 ms 716 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Runtime error 1 ms 332 KB Execution killed with signal 11
61 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 0 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 716 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 1 ms 716 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 716 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 648 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 1 ms 716 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 1 ms 716 KB Output is correct
36 Correct 1 ms 716 KB Output is correct
37 Correct 1 ms 716 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 716 KB Output is correct
40 Correct 1 ms 716 KB Output is correct
41 Correct 1 ms 716 KB Output is correct
42 Correct 1 ms 716 KB Output is correct
43 Correct 1 ms 716 KB Output is correct
44 Correct 1 ms 716 KB Output is correct
45 Correct 1 ms 716 KB Output is correct
46 Correct 1 ms 716 KB Output is correct
47 Correct 1 ms 716 KB Output is correct
48 Correct 1 ms 716 KB Output is correct
49 Correct 1 ms 716 KB Output is correct
50 Correct 1 ms 716 KB Output is correct
51 Correct 1 ms 716 KB Output is correct
52 Correct 1 ms 716 KB Output is correct
53 Correct 1 ms 716 KB Output is correct
54 Correct 1 ms 716 KB Output is correct
55 Correct 1 ms 716 KB Output is correct
56 Correct 1 ms 716 KB Output is correct
57 Correct 1 ms 716 KB Output is correct
58 Correct 1 ms 716 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Runtime error 1 ms 332 KB Execution killed with signal 11
61 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 716 KB Output is correct
3 Correct 1 ms 716 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 1 ms 716 KB Output is correct
7 Correct 0 ms 716 KB Output is correct
8 Correct 1 ms 716 KB Output is correct
9 Correct 1 ms 716 KB Output is correct
10 Correct 1 ms 716 KB Output is correct
11 Correct 1 ms 716 KB Output is correct
12 Correct 1 ms 716 KB Output is correct
13 Correct 1 ms 716 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 716 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 716 KB Output is correct
24 Correct 1 ms 716 KB Output is correct
25 Correct 1 ms 716 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 648 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 1 ms 716 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 1 ms 716 KB Output is correct
36 Correct 1 ms 716 KB Output is correct
37 Correct 1 ms 716 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 716 KB Output is correct
40 Correct 1 ms 716 KB Output is correct
41 Correct 1 ms 716 KB Output is correct
42 Correct 1 ms 716 KB Output is correct
43 Correct 1 ms 716 KB Output is correct
44 Correct 1 ms 716 KB Output is correct
45 Correct 1 ms 716 KB Output is correct
46 Correct 1 ms 716 KB Output is correct
47 Correct 1 ms 716 KB Output is correct
48 Correct 1 ms 716 KB Output is correct
49 Correct 1 ms 716 KB Output is correct
50 Correct 1 ms 716 KB Output is correct
51 Correct 1 ms 716 KB Output is correct
52 Correct 1 ms 716 KB Output is correct
53 Correct 1 ms 716 KB Output is correct
54 Correct 1 ms 716 KB Output is correct
55 Correct 1 ms 716 KB Output is correct
56 Correct 1 ms 716 KB Output is correct
57 Correct 1 ms 716 KB Output is correct
58 Correct 1 ms 716 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Runtime error 1 ms 332 KB Execution killed with signal 11
61 Halted 0 ms 0 KB -