#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200
int min(int a, int 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;
}
int dd1[N], dd2[N];
void dfs1(int p, int i) {
int o;
dd1[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs1(i, j);
dd1[i] = max(dd1[i], dd1[j] + 1);
}
}
}
void dfs2(int p, int i, int d) {
int o, d1, d2, j1;
d1 = d2 = 0, j1 = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
if (d1 < dd1[j])
d2 = d1, d1 = dd1[j], j1 = j;
else if (d2 < dd1[j])
d2 = dd1[j];
}
}
dd2[i] = d;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs2(i, j, max(d, j == j1 ? d2 : d1) + 1);
}
}
char dp[N][N + 1], dq[N + 1]; int ll[N], rr[N];
void dfs3(int p, int i) {
static int jj[N], dd[N + 1];
int m, o, h, di, dj, d;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs3(i, j);
}
m = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
jj[m++] = j;
}
dd[m] = dd2[i];
for (h = m - 1; h >= 0; h--) {
int j = jj[h];
dd[h] = max(dd[h + 1], dd1[j]);
}
ll[i] = max(k - 1 - dd[0], 0), rr[i] = max(ll[i], 1);
if (ll[i] >= rr[i])
dp[i][ll[i]] = 1;
else
dp[i][0] = dp[i][1] = 1;
for (h = 0; h < m; h++) {
int j = jj[h], l_, r_;
memset(dq, 0, (n + 1) * sizeof *dq);
l_ = max(k - 1 - dd[h + 1], 0), r_ = max(max(rr[i], rr[j] + 1), l_);
for (di = ll[i]; di <= rr[i]; di++)
for (dj = ll[j]; dj <= rr[j]; dj++)
if (dp[i][di] && dp[j][dj]) {
int good, di_, dj_;
good = 1;
if (ll[i] < rr[i] && ll[j] < rr[j])
for (d = 0; d <= k; d++)
if (d <= rr[i] && k - d <= rr[j] && (d <= di ? 0 : 1) + (k - d <= dj ? 0 : 1) != 1) {
good = 0;
break;
}
if (!good)
continue;
di_ = max(di, l_), dj_ = max(dj + 1, l_);
if (di == 0)
dq[di] = 1;
else if (min(di_, min(rr[i], rr[j] + 1)) == min(dj_, min(rr[i], rr[j] + 1)))
dq[min(max(di_, dj_), r_)] = 1;
}
memcpy(dp[i], dq, (n + 1) * sizeof *dq);
ll[i] = l_, rr[i] = r_;
}
}
int main() {
int h, i, j;
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);
}
dfs1(-1, 0);
dfs2(-1, 0, 0);
dfs3(-1, 0);
printf(dp[0][ll[0]] ? "YES\n" : "NO\n");
printf("0\n");
return 0;
}
Compilation message
wells.c: In function 'append':
wells.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
wells.c: In function 'main':
wells.c:122:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
wells.c:126:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
204 KB |
Output is partially correct |
2 |
Partially correct |
1 ms |
288 KB |
Output is partially correct |
3 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
4 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
5 |
Partially correct |
1 ms |
324 KB |
Output is partially correct |
6 |
Partially correct |
1 ms |
284 KB |
Output is partially correct |
7 |
Correct |
0 ms |
276 KB |
Output is correct |
8 |
Partially correct |
0 ms |
332 KB |
Output is partially correct |
9 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Incorrect |
0 ms |
288 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
204 KB |
Output is partially correct |
2 |
Partially correct |
1 ms |
288 KB |
Output is partially correct |
3 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
4 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
5 |
Partially correct |
1 ms |
324 KB |
Output is partially correct |
6 |
Partially correct |
1 ms |
284 KB |
Output is partially correct |
7 |
Correct |
0 ms |
276 KB |
Output is correct |
8 |
Partially correct |
0 ms |
332 KB |
Output is partially correct |
9 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Incorrect |
0 ms |
288 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
204 KB |
Output is partially correct |
2 |
Partially correct |
1 ms |
288 KB |
Output is partially correct |
3 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
4 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
5 |
Partially correct |
1 ms |
324 KB |
Output is partially correct |
6 |
Partially correct |
1 ms |
284 KB |
Output is partially correct |
7 |
Correct |
0 ms |
276 KB |
Output is correct |
8 |
Partially correct |
0 ms |
332 KB |
Output is partially correct |
9 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Incorrect |
0 ms |
288 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
204 KB |
Output is partially correct |
2 |
Partially correct |
1 ms |
288 KB |
Output is partially correct |
3 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
4 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
5 |
Partially correct |
1 ms |
324 KB |
Output is partially correct |
6 |
Partially correct |
1 ms |
284 KB |
Output is partially correct |
7 |
Correct |
0 ms |
276 KB |
Output is correct |
8 |
Partially correct |
0 ms |
332 KB |
Output is partially correct |
9 |
Partially correct |
1 ms |
332 KB |
Output is partially correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Incorrect |
0 ms |
288 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |