Submission #500674

# Submission time Handle Problem Language Result Execution time Memory
500674 2021-12-31T18:34:23 Z rainboy Wells (CEOI21_wells) C
0 / 100
32 ms 296 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200
#define K	200
#define INF	0x3f3f3f3f
#define MD	1000000007

int max(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;
}

int dp[N], dq[N], d_;

void dfs1(int p, int i) {
	int o;

	dp[i] = 1;
	if (p == -1)
		d_ = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs1(i, j);
			if (p == -1)
				d_ = max(d_, dp[i] + dp[j]);
			dp[i] = max(dp[i], dp[j] + 1);
		}
	}
	if (p == -1)
		d_ = max(d_, dp[i]);
}

int dr[N];
char useless[N];

int check(int p, int i, int d, int d_) {
	int o, q1, q2;

	if (useless[i]) {
		dp[i] = -INF, dq[i] = -INF, dr[i] = -INF;
		return 1;
	}
	dp[i] = 1, dq[i] = -INF, dr[i] = 1, q1 = q2 = -INF;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			if (!check(i, j, (d + 1) % d_, d_))
				return 0;
			dp[i] = max(dp[i], dp[j] + 1);
			dq[i] = max(dq[i], dq[j]);
			if (d != 0 && dr[i] + dr[j] >= d_)
				return 0;
			dr[i] = max(dr[i], dr[j] + 1);
			if (q1 < dq[j])
				q2 = q1, q1 = dq[j];
			else if (q2 < dq[j])
				q2 = dq[j];
		}
	}
	if (d != 0 && dr[i] >= d_)
		return 0;
	if (d == 0)
		dq[i] = dp[i], dr[i] = 0;
	if (d != 0 && q2 != -INF && (d_ - d) * 2 + 1 <= d_ && (d_ - d) * 2 - 1 + q1 + q2 >= d_)
		return 0;
	return 1;
}

int cc[N];

void mark(int p, int i, int d, int d_) {
	int o;

	cc[i] = d == 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			mark(i, j, (d + 1) % d_, d_);
	}
}

int check_(int p, int i, int d, int c) {
	int o;

	c += cc[i];
	if (--d == 0)
		return c == 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && !check_(i, j, d, c))
			return 0;
	}
	return 1;
}

int main() {
	static int qu[N];
	int n, d, h, i, j, k, o, ans, done;

	scanf("%d%d", &n, &d);
	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);
	}
	done = 1;
	for (i = 0; i < n; i++) {
		d_ = 0, dfs1(-1, i);
		if (d_ < d)
			useless[i] = 1;
		else
			done = 0;
		if (dp[i] >= d)
			for (h = 0, j = i; h < d; h++) {
				qu[h] = j;
				for (o = eo[j]; o--; ) {
					k = ej[j][o];
					if (dp[k] == dp[j] - 1) {
						j = k;
						break;
					}
				}
			}
	}
	ans = 1;
	if (!done) {
		ans = 0;
		for (h = 0; h < d; h++) {
#if 0
			if (check(-1, qu[h], 0, d))
				ans++;
#else
			mark(-1, qu[h], 0, d);
			ans++;
			for (i = 0; i < n; i++)
				if (!check_(-1, i, d, 0)) {
					ans--;
					break;
				}
		}
		if (ans == 0) {
			printf("NO\n");
			printf("0\n");
			return 0;
		}
#endif
	}
	for (i = 0; i < n; i++)
		if (useless[i])
			ans = ans * 2 % MD;
	printf("YES\n");
	printf("%d\n", ans);
	return 0;
}

Compilation message

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 'main':
wells.c:113:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |  scanf("%d%d", &n, &d);
      |  ^~~~~~~~~~~~~~~~~~~~~
wells.c:117:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 5 ms 204 KB Output is correct
3 Partially correct 32 ms 276 KB Output is partially correct
4 Correct 2 ms 204 KB Output is correct
5 Partially correct 6 ms 204 KB Output is partially correct
6 Correct 9 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Partially correct 7 ms 288 KB Output is partially correct
17 Partially correct 5 ms 288 KB Output is partially correct
18 Partially correct 4 ms 204 KB Output is partially correct
19 Partially correct 4 ms 204 KB Output is partially correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 3 ms 204 KB Output is correct
22 Partially correct 7 ms 204 KB Output is partially correct
23 Partially correct 21 ms 204 KB Output is partially correct
24 Partially correct 18 ms 204 KB Output is partially correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 2 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Partially correct 2 ms 204 KB Output is partially correct
37 Correct 9 ms 204 KB Output is correct
38 Correct 10 ms 292 KB Output is correct
39 Correct 5 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Partially correct 3 ms 204 KB Output is partially correct
43 Partially correct 6 ms 204 KB Output is partially correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 7 ms 296 KB Output is correct
47 Correct 3 ms 204 KB Output is correct
48 Partially correct 2 ms 204 KB Output is partially correct
49 Incorrect 1 ms 204 KB Output isn't correct
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 5 ms 204 KB Output is correct
3 Partially correct 32 ms 276 KB Output is partially correct
4 Correct 2 ms 204 KB Output is correct
5 Partially correct 6 ms 204 KB Output is partially correct
6 Correct 9 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Partially correct 7 ms 288 KB Output is partially correct
17 Partially correct 5 ms 288 KB Output is partially correct
18 Partially correct 4 ms 204 KB Output is partially correct
19 Partially correct 4 ms 204 KB Output is partially correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 3 ms 204 KB Output is correct
22 Partially correct 7 ms 204 KB Output is partially correct
23 Partially correct 21 ms 204 KB Output is partially correct
24 Partially correct 18 ms 204 KB Output is partially correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 2 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Partially correct 2 ms 204 KB Output is partially correct
37 Correct 9 ms 204 KB Output is correct
38 Correct 10 ms 292 KB Output is correct
39 Correct 5 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Partially correct 3 ms 204 KB Output is partially correct
43 Partially correct 6 ms 204 KB Output is partially correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 7 ms 296 KB Output is correct
47 Correct 3 ms 204 KB Output is correct
48 Partially correct 2 ms 204 KB Output is partially correct
49 Incorrect 1 ms 204 KB Output isn't correct
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 5 ms 204 KB Output is correct
3 Partially correct 32 ms 276 KB Output is partially correct
4 Correct 2 ms 204 KB Output is correct
5 Partially correct 6 ms 204 KB Output is partially correct
6 Correct 9 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Partially correct 7 ms 288 KB Output is partially correct
17 Partially correct 5 ms 288 KB Output is partially correct
18 Partially correct 4 ms 204 KB Output is partially correct
19 Partially correct 4 ms 204 KB Output is partially correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 3 ms 204 KB Output is correct
22 Partially correct 7 ms 204 KB Output is partially correct
23 Partially correct 21 ms 204 KB Output is partially correct
24 Partially correct 18 ms 204 KB Output is partially correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 2 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Partially correct 2 ms 204 KB Output is partially correct
37 Correct 9 ms 204 KB Output is correct
38 Correct 10 ms 292 KB Output is correct
39 Correct 5 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Partially correct 3 ms 204 KB Output is partially correct
43 Partially correct 6 ms 204 KB Output is partially correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 7 ms 296 KB Output is correct
47 Correct 3 ms 204 KB Output is correct
48 Partially correct 2 ms 204 KB Output is partially correct
49 Incorrect 1 ms 204 KB Output isn't correct
50 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 5 ms 204 KB Output is correct
3 Partially correct 32 ms 276 KB Output is partially correct
4 Correct 2 ms 204 KB Output is correct
5 Partially correct 6 ms 204 KB Output is partially correct
6 Correct 9 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Partially correct 7 ms 288 KB Output is partially correct
17 Partially correct 5 ms 288 KB Output is partially correct
18 Partially correct 4 ms 204 KB Output is partially correct
19 Partially correct 4 ms 204 KB Output is partially correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 3 ms 204 KB Output is correct
22 Partially correct 7 ms 204 KB Output is partially correct
23 Partially correct 21 ms 204 KB Output is partially correct
24 Partially correct 18 ms 204 KB Output is partially correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 2 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 2 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Partially correct 2 ms 204 KB Output is partially correct
37 Correct 9 ms 204 KB Output is correct
38 Correct 10 ms 292 KB Output is correct
39 Correct 5 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Partially correct 3 ms 204 KB Output is partially correct
43 Partially correct 6 ms 204 KB Output is partially correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 7 ms 296 KB Output is correct
47 Correct 3 ms 204 KB Output is correct
48 Partially correct 2 ms 204 KB Output is partially correct
49 Incorrect 1 ms 204 KB Output isn't correct
50 Halted 0 ms 0 KB -