#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;
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)
dq[i] = max(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 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 (check(-1, qu[h], 0, d))
ans++;
if (ans == 0) {
printf("NO\n");
printf("0\n");
return 0;
}
}
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:78:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d%d", &n, &d);
| ^~~~~~~~~~~~~~~~~~~~~
wells.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
6 |
Correct |
1 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 |
1 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 |
1 ms |
204 KB |
Output is correct |
16 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
17 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
18 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
19 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
23 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
24 |
Partially correct |
1 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 |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 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 |
1 ms |
204 KB |
Output is partially correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
288 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
2 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Partially correct |
2 ms |
204 KB |
Output is partially correct |
43 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
44 |
Correct |
2 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
49 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
6 |
Correct |
1 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 |
1 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 |
1 ms |
204 KB |
Output is correct |
16 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
17 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
18 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
19 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
23 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
24 |
Partially correct |
1 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 |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 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 |
1 ms |
204 KB |
Output is partially correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
288 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
2 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Partially correct |
2 ms |
204 KB |
Output is partially correct |
43 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
44 |
Correct |
2 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
49 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
6 |
Correct |
1 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 |
1 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 |
1 ms |
204 KB |
Output is correct |
16 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
17 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
18 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
19 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
23 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
24 |
Partially correct |
1 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 |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 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 |
1 ms |
204 KB |
Output is partially correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
288 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
2 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Partially correct |
2 ms |
204 KB |
Output is partially correct |
43 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
44 |
Correct |
2 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
49 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
6 |
Correct |
1 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 |
1 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 |
1 ms |
204 KB |
Output is correct |
16 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
17 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
18 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
19 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
23 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
24 |
Partially correct |
1 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 |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 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 |
1 ms |
204 KB |
Output is partially correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
288 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
2 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Partially correct |
2 ms |
204 KB |
Output is partially correct |
43 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
44 |
Correct |
2 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Partially correct |
1 ms |
204 KB |
Output is partially correct |
49 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |