Ural 1018 Binary Apple Tree [树形dp]

Binary Apple Tree
Time limit: 1.0 second
Memory limit: 64 MB

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

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


要求保留k个枝条最多可以获得的收益
更新每个节点的时候 , dp[u][k] 都保存了以前的兄弟所能达到的最大值,只需要更新此处的就可以了,枚举分 j 个枝条给当前 v 子树。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
const int INF=0x3f3f3f3f;
const int maxn=105;
struct Edge
{
    int to,next;
    int cost;
}edge[maxn*maxn];
int head[maxn];
int maxedge;
inline void addedge(int u,int v,int c)
{
    edge[++maxedge]=(Edge){v,head[u],c};
    head[u]=maxedge;
    edge[++maxedge]=(Edge){u,head[v],c};
    head[v]=maxedge;
}
int n,m;
inline void init()
{
    scanf("%d%d",&n,&m);
    memset(head,-1,sizeof(head));
    maxedge=-1;
    for(int i=1;i^n;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        addedge(x,y,z);
    }
}
int f[maxn][maxn];
int dfs(int u,int father)
{
    int branch=0;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==father) continue;
        branch+=dfs(v,u)+1;
        for(int k=min(branch,m);k;k--)
            for(int j=k;j;j--) // give j branches for v
                smax( f[u][k] , f[u][k-j] + f[v][j-1] + edge[i].cost ); // the node v un-used included in f[u][k]
    }
    return branch;
}
int main()
{
    freopen("apple.in","r",stdin);
    freopen("apple.out","w",stdout);
    init();
    dfs(1,-1);
    printf("%d",f[1][m]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值