1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <cstdio> #include <algorithm> using namespace std; const int N = 160, M = 310; int n, m, d[N], f[N][N]; int idx, hd[N], nxt[M], edg[M]; void dfs(int x, int fa) { f[x][1] = d[x]; for (int i = hd[x]; ~i; i = nxt[i]) { if (edg[i] == fa) continue; dfs(edg[i], x); for (int j = m; j; j--) for (int k = 1; k < j; k++) f[x][j] = min(f[x][j], f[x][k] + f[edg[i]][j - k] - 2); } } void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) hd[i] = -1; for (int i = 1, a, b; i < n; i++) { scanf("%d%d", &a, &b); d[a]++, d[b]++; add(a, b); add(b, a); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) f[i][j] = n; dfs(1, 0); int res = n; for (int i = 1; i <= n; i++) res = min(res, f[i][m]); printf("%d", res); return 0; }
|