ural 1018 Binary Apple Tree(树形dp)

Description

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 😉

Input

5 2
1 3 1
1 4 10
2 3 20
3 5 20

Output

21

题意

给一棵边有权值的二叉树,节点编号为1~n,1是根节点。求砍掉一些边,只保留q条边,这q条边构成的子树的根节点要求是1,问这颗子树的最大权值是多少?

题解

dp(i, j) 表示子树i,保留j个节点(注意是节点)的最大权值。每条边的权值,把它看作是连接的两个节点中的儿子节点的权值。那么,就可以对所有i的子树做分组背包,即每个子树可以选择1,2,…j-1条边分配给它。

状态转方程:

dp(i, j) = max{max{dp(i, j-k) +dp(v, k) | 1<=k<j} | v是i的儿子}
ans = f(1, q+1)

AC代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;
int n,q;
vector<pair<int,int>>adj[maxn];
int tot[maxn];
int dp[maxn][maxn];
int dfs(int u,int fa){
	tot[u]=1;
	for(int i=0;i<adj[u].size();i++){
		int v=adj[u][i].first;
		if(v==fa) continue;
		tot[u]+=dfs(v,u);
	}
	for(int i=0;i<adj[u].size();i++){
		int v=adj[u][i].first;
		int w=adj[u][i].second;
		if(v==fa) continue;
		for (int i=tot[u];i>0;i--){
            for(int j=1;j<i&&j<=tot[v];j++){
                dp[u][i]=max(dp[u][i],dp[u][i-j]+dp[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(make_pair(v,w));
			adj[v].push_back(make_pair(u,w));
			memset(dp,0,sizeof(dp));
			dfs(1,-1);
		}	
		printf("%d\n",dp[1][q+1]);
		return 0;
	}
}

参考博客:https://blog.csdn.net/shuangde800/article/details/10834207

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值