连接:1018. 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
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

题目意思: 
有一棵苹果树,苹果树的是一棵二叉树,共N个节点,树节点编号为1~N,编号为1的节点为树根,边可理解为树的分枝,每个分支都长着若干个苹果,现在要要求减去若干个分支,保留M个分支,要求这M个分支的苹果数量最多。
二叉苹果树:一道金典的树形DP,这题很特殊,因为是二叉树,所以只需要处理左二子,右儿子就可以了,但是我还是想着用一般的树形DP来做这道题,就是不当成二叉树来做。
思路:跟0-1背包思想差不多,在u的儿子v为根节点的子树中选j条边加到u中。
dp[u][k]=max(dp[u][k],dp[u][k-j]+dp[v][j-1]+w)(1<j<=k),w:u与v的边的取值,因为如果在v子树中选边,那么u到v的边必选。



#include<stdio.h>
#include<string.h>
const int N=110;
int dp[N][N],vis[N],head[N],num,m;
struct edge
{
int st,ed,w,next;
}e[N*4];
void addedge(int x,int y,int w)
{
e[num].st=x;e[num].ed=y;e[num].w=w;e[num].next=head[x];head[x]=num++;
e[num].st=y;e[num].ed=x;e[num].w=w;e[num].next=head[y];head[y]=num++;
}
void dfs(int u)
{
vis[u]=1;
int i,v,w,j,k,son=0;
for(i=head[u];i!=-1;i=e[i].next)
{
v=e[i].ed;w=e[i].w;
if(vis[v]==1)continue;
dfs(v);
for(k=m;k>=1;k--)//0-1背包
{
for(j=1;j<=k;j++)//在v节点的子树中选择j条边
if(dp[u][k]<dp[u][k-j]+dp[v][j-1]+w)//u与v有一条边,所以加上dp[v][j-1],
dp[u][k]=dp[u][k-j]+dp[v][j-1]+w;
}
}
}
int main()
{
int i,x,y,w,n;
while(scanf("%d%d",&n,&m)!=-1)
{
memset(head,-1,sizeof(head));
num=0;
for(i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
addedge(x,y,w);
}
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
dfs(1);
printf("%d\n",dp[1][m]);
}
return 0;
}



URAL 1018 (金典树形DP)的更多相关文章

  1. Ural 1018 (树形DP+背包+优化)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题目大意:树枝上间连接着一坨坨苹果(不要在意'坨'),给 ...

  2. ural 1018(树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 思路:典型的树形dp,处理的时候类似于分组背包,dp[i] ...

  3. ural 1018 Binary Apple Tree(树形dp | 经典)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  4. 树形DP URAL 1039 Anniversary Party

    题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...

  5. 刷题总结——二叉苹果树(ssoj树形dp+记忆化搜索)

    题目: 题目背景 URAL:http://acm.timus.ru/problem.aspx?space=1&num=1018 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说 ...

  6. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  7. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  8. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  9. hdu1520 第一道树形DP,激动哇咔咔!

    A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

随机推荐

  1. 详解python2 和 python3的区别

    看到这个题目大家可能猜到了我接下来要讲些什么,呵呵,对了,那就是列出这两个不同版本间的却别!搜索一下大家就会知道,python有两个主要的版本,python2 和 python3 ,但是python又 ...

  2. json_response的用法

    传统的方法是当我们处理一个表单时,我们Post数据给服务器,服务器对数据进行处理后将数据返回给用户,此时部分写法是用页面刷新的方式将页面重新刷新一次呈现给用户,这样的话用户相当于读入了两次页面,人一多 ...

  3. Week7(10月21日)

    Part I:提问  =========================== 1.请为下图编写视图代码,视图中表单提交后,交给当前控制器和动作处理. 2.如何实现点击列标题排序功能? 3.分页时采用了 ...

  4. 窗体区域绘制问题WS_CLIPCHILDREN与WS_CLIPSIBLINGS

    WS_CLIPCHILDREN,使得父窗体在绘制时留出其上的子窗体的位置不去画它,而那片区域留着子窗体去画.WS_CLIPSIBLINGS,必须用于子窗体,使得该子窗体在收到WM_PAINT时同时令其 ...

  5. 作为Qt 合作伙伴的V-Play,比大家都领先了一步 planet.qt.io

    今天发布博客,将Flappy Bird和其它的小游戏移植到Respberry PI了 http://planet.qt.io/ planet.qt.io 的repo: https://coderevi ...

  6. 转:C++ 匿名namespace的作用以及它与static的区别

    匿名namespace的作用以及它与static的区别 一.匿名namespace的作用在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做为函数名或者全局变量名,则 ...

  7. 查看linux下各数据类型的大小

    代码如下: #include<stdio.h> int main() { printf("int:%d bytes\n",sizeof(int)); printf(&q ...

  8. PHP升级之后$SESSION丢失

    要在生产环境为一个内部系统升PHP版本,由5.3升成5.4.16 生成以后发现不能login,一路打断点过去,发现服务器端两个页面跳转的时候,取不到$SESSION 悲催的上网找解决方案,结果发现各种 ...

  9. Linux下arp用法

    [功能] 管理系统的arp缓存. [描述] 用来管理系统的arp缓存,常用的命令包括: arp: 显示所有的表项. arp  -d  address: 删除一个arp表项. arp  -s addre ...

  10. Asp.Net中GridView加入鼠标滑过的高亮效果和单击行颜色改变

    转载自:http://www.cnblogs.com/fly_dragon/archive/2010/09/03/1817252.html protected void GridView1_RowDa ...