This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 && dr[i] >= d_)
return 0;
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 (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 'main':
wells.c:80:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d%d", &n, &d);
| ^~~~~~~~~~~~~~~~~~~~~
wells.c:84:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |