Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enumerate by integers the root of binary apple tree, points of branching and the ends of twigs. This way we may distinguish different branches by their ending points. We will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1 to
N, where
N is the total number of all enumerated points. For instance in the picture below
N is equal to 5. Here is an example of an enumerated tree with four branches:
2 5 \ / 3 4 \ / 1 |
As you may know it's not convenient to pick an apples from a tree when there are too much of branches. That's why some of them should be removed from a tree. But you are interested in removing branches in the way of minimal loss of apples. So your are given amounts of apples on a branches and amount of branches that should be preserved. Your task is to determine how many apples can remain on a tree after removing of excessive branches.
Input
First line of input contains two numbers:
N and
Q (
2 ≤
N ≤ 100;
1 ≤
Q ≤
N − 1
).
N denotes the number of enumerated points in a tree.
Q denotes amount of branches that should be preserved. Next
N − 1 lines contains descriptions of branches. Each description consists of a three integer numbers divided by spaces. The first two of them define branch by it's ending points. The third number defines the number of apples on this branch. You may assume that no branch contains more than 30000 apples.
Output
Output should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-)
Sample
input | output |
---|---|
5 2 1 3 1 1 4 10 2 3 20 3 5 20 | 21 |
非常经典的一道树形DP,题目意思是有一颗N个节点的二叉树,每一条边都有一个权值,要从中选择M个点的联通块,使其权值最大。很容易想到,可以将边权转化为点权,然后用DFS将图转化为有根树。
不用记忆化搜索就会TLE!
DP(i,j) = w(i) j=0
= dp(rson[i],k)+dp(lson[i],j-k-2)+w[i] k>=0&&k<=j-2 取最大
= max(dp(rson[i],j-1),dp(lson[i],j-1))+w[i] j>=1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100+10;
int lson[maxn],rson[maxn],w[maxn],tmp[maxn][maxn];
vector<int> g[maxn];
int dp[maxn][maxn];
bool vis[maxn];
int m,n;
void init(){
memset(dp,-1,sizeof(dp));
memset(w,0,sizeof(w));
memset(lson,-1,sizeof(lson));
memset(rson,-1,sizeof(rson));
memset(vis,0,sizeof(vis));
for(int i = 0; i < maxn; i++)
g[i].clear();
}
int dfs(int i,int j){
if(j==0)
return w[i];
else{
if(dp[i][j]!=-1) return dp[i][j];
int a = max(dfs(lson[i],j-1),dfs(rson[i],j-1))+w[i];
int b = 0;
for(int k = 0; k <= j-2; k++){
b = max(dfs(rson[i],k)+dfs(lson[i],j-k-2)+w[i],b);
}
return dp[i][j] = max(a,b);
}
}
void ddfs(int x){
for(int i = 0; i < g[x].size(); i++){
if(!vis[g[x][i]]){
vis[g[x][i]] = 1;
if(lson[x]==-1){
lson[x] = g[x][i];
w[g[x][i]] = tmp[x][g[x][i]];
ddfs(g[x][i]);
}
else if(rson[x]==-1){
rson[x] = g[x][i];
w[g[x][i]] = tmp[x][g[x][i]];
ddfs(g[x][i]);
}
else
return;
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i = 0; i < n-1; i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(b);
g[b].push_back(a);
tmp[a][b] = tmp[b][a] = c;
}
vis[1] = 1;
ddfs(1);
cout<<dfs(1,m)<<endl;
}
return 0;
}