Ural 1018 Binary Apple Tree (树形dp)

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, whereN is the total number of all enumerated points. For instance in the picture belowN 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 ;-)

题目大意:题目中给定了一棵根节点为1的标准二叉树(所有非叶子节点都有两个儿子)。每一条边有一定的边权。问保留Q条树枝的情况下最大的权值为多少?


解题思路:按照题目要求建树,因为是标准二叉树,所以我们可以开两个数组记录每个节点的左儿子和右儿子。根据题目问题可以设计基本状态 dp[i][j],表示在第i节点下包含j条树枝最多可有多少个苹果。

根据状态易得状态转移方程:
                                 |  0  ,              j = 0 || i为叶子节点
                dp[i][j] =   |  仅保留左子树或右子树
                                 |  由左右子树共同组成

AC代码:
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 105
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int n,q;
int ans;
int a[maxn][maxn];//存储边权
int dp[maxn][maxn];//dp[i][j] -> 在第i节点下包含j条树枝最多可有多少个苹果
int l[maxn],r[maxn];//存储每个节点的左右子节点
int x,y,z;
vector<int> e[maxn];

void dfs(int u, int fa)// build a tree
{
	for(int i = 0; i<e[u].size(); i++)
	{
		int v = e[u][i];
		if(v==fa)	continue;
		if(l[u]==0)
			l[u] = v;
		else
			r[u] = v;
		dfs(v, u);
	}
}

int cal(int u, int num)
{
	if(num==0 || l[u]+r[u]==0)	return 0;
	if(dp[u][num])	return dp[u][num];
	for(int i = 0; i<=num-2; i++)
	{
		dp[u][num] = max(dp[u][num], a[u][l[u]] + a[u][r[u]] + cal(l[u], i) + cal(r[u], num -2 - i));
	}
	dp[u][num] = max(dp[u][num], a[u][l[u]] + cal(l[u], num-1) );
	dp[u][num] = max(dp[u][num], a[u][r[u]] + cal(r[u], num-1) );
	return dp[u][num];
}
int main()
{
    //freopen("test.txt","r",stdin);
    cin>>n>>q;
    for(int i = 1; i<n; i++)
    {
    	cin>>x>>y>>z;
    	a[x][y] = a[y][x] = z;
    	e[x].push_back(y);
    	e[y].push_back(x);
    }
    dfs(1,-1);
    cout << cal(1,q) << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值