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 <iostream> #include <cstring> #include <cstdio> #include <vector> using namespace std;
const int N = 110; int n, m;
struct Node{ int to, val; }; vector<Node> edge[N]; int dp[N][N]; int tmp[N]; int sz[N];
void dfs(int x, int f){ for(int i=0;i<edge[x].size();i++){ int y = edge[x][i].to, val = edge[x][i].val; if (y==f) continue; dfs(y, x); sz[x] += sz[y] + 1; memset(tmp, 0, sizeof tmp); int mi= min(sz[x], m); for(int j=0;j<=mi;j++) for(int t=1;t<=j;t++) tmp[j] = max(tmp[j], dp[y][t-1] + dp[x][j-t] + val); for(int j=1;j<=m;j++) dp[x][j] = max(dp[x][j], tmp[j]); } }
int main(){ scanf("%d%d", &n, &m); for(int i=1;i<=n-1;i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); edge[a].push_back((Node){b, c}); edge[b].push_back((Node){a, c}); }
dfs(1, 0); printf("%d\n", dp[1][m]); return 0; }
|