# P2015 二叉苹果树

f[u][j] = max(f[u][j] , f[v][k] + f[u][j-k] )

f[u][j] = max(f[u][j] , f[v][k] + f[u][j-k-1] + val )    val 就是父亲和儿子连接的边权

```#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>
#include <cmath>

#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1

const double eps = 1e-10;
const int maxn = 310;
const LL mod = 1e9 + 7;

int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;}
using namespace std;

struct edge {
int v,nxt;
}e[maxn];

int c[maxn][maxn],f[maxn][maxn];
int cnt,n,q;

e[++cnt].v = v;
}

void dfs(int x) {
for (int i = head[x];~i;i = e[i].nxt) {
int v = e[i].v;
dfs(v);
for (int j = q;j >= 1;j--) {
for (int k = j-1;k >= 0;k--)
f[x][j] = max(f[x][j],f[v][k]+f[x][j-k-1]+c[x][v]);
}
}
}

int main() {
cnt = 0;
cin >> n >> q;
for (int i = 1;i < n;i++) {
int u,v,w;
cin >> u >> v >> w;
c[u][v] = w;
}
dfs(1);
cout << f[1][q] << endl;
return 0;
}```

P2015 二叉苹果树

(0)
(0)