Meaning
A two-fork tree with a value for the edge, and the node number is 1~n,1 is the root node. To cut off some of the edges, just keep the Q-side, the Q-Edge of the subtree
The root node requirement is 1, ask what is the maximum weight of this subtree?
Ideas
A very classic tree-type DP problem, according to my current problem, there are many ways are derived from this problem.
F (I, j) represents the subtree I, preserving the maximum weight of the J node (note is the node). The weight of each edge is considered to be the weight of the son node in the two connected nodes.
Then, you can pack all the subtree of I, that is, each subtree can select 1,2,... j-1 to assign to it.
State transitions are:
F (i, j) = max{max{f (i, j-k) + f (V, k) | 1<=k<j} | V is the son of I}
ans = f (1, q+1)
Code
/**===================================================== * is a solution for ACM/ICPC problem * * @source: URA l-1018 Binary Apple trees * @description: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshu
angde@gmail.com * Copyright (C) 2013/09/01 18:43 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> #
Include <algorithm> #include <vector> #include <cstring> #define MP make_pair using namespace std;
typedef pair<int, int>pii;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
const int MAXN = 110;
vector<pii>adj[maxn];
int n, q;
int TOT[MAXN];
int F[MAXN][MAXN]; http://www.bianceng.cn int Dfs (int u, int fa) {Tot[u] = 1; for (int e = 0; e < adj[u].size (); ++e) {int v = a
Dj[u][e].first;
if (v = = FA) continue;
Tot[u] + = DFS (v, u); for (int e = 0; e < adj[u].size (); ++e) {int v = adj[u][e].first; int w = adj[U]
[E].second;
if (v = = FA) continue; for (int i = tot[u]; i > 0-i) {for (int j = 1; J < i && J <= Tot[v]; ++j) {F[u][i] = max (f[u][i), f[
U][I-J] + f[v][j] + W);
}} return Tot[u];
int main () {while (~scanf ("%d%d", &n, &q)) {for (int i = 0; I <= N; ++i) adj[i].clear (); for (int i = 0; i < n-1 ++i) {int u, V, W; scanf ("%d%d%d", &u, &v, &w); Adj[u].push_back (MP (V, W));
Dj[v].push_back (MP (U, W));
} memset (f, 0, sizeof (f));
DFS (1,-1);
printf ("%d\n", f[1][q+1]);
return 0; }