连接: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. hdu4597 Play Game

    Play Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  2. Ext JS学习第五天 我们所熟悉的javascript(四)

    此文用来记录学习笔记: •javascript之对象.面向对象 •可能对于高级语言你可能了解甚至精通OOP面向对象,那么对于javascript你又熟悉多少呢?我们一起来学习javascript面向对 ...

  3. c++,多继承造成的二义性及解决办法

    #include <iostream> using namespace std; //------------------------------- class A1{ public: i ...

  4. vim下设置tab

    前言:大多数情况下tab键的宽度设置为4个空格,这个可以根据自己 的代码风格进行替换,然而当你提交不同的语言的代码的时候python 和c的时候就有区别了.c的话一般tab键做缩进,而python提交 ...

  5. 仿爱乐透android客户端界面实现(附工程源码)

    最近研究了爱乐透android客户端的界面,感觉它的界面布局在一般开发中具有代表性.难点在于复杂的布局实现. 界面实现主要采用了以下方式: 注意:版本支持:android2.2以上,低版本要改动源码哦 ...

  6. POJ 3047 Fibonacci

    DEBUG很辛苦,且行, 且珍惜 原代码: ans[0][0] = (ans[0][0] * a[flag][0][0] + ans[0][1] * a[flag][1][0]) % 10000; a ...

  7. CSS 规避脱标之两种用法

    大家好,我是小强老师,今天讲解一小点知识哈 对比了才知道什么好 看不出,很漂亮吧! 有木有倾国倾城的美色. 呵呵,好多东西也是这样的,好的东西只有对比了才觉得好. 我们知道我们网页布局 有三模式.   ...

  8. 【细说Java】揭开Java的main方法神秘的面纱(转)

    大家都知道,main方法是Java应用程序的入口,其定义格式为: public static void main(String[] args) 可是为什么要这么定义呢?不这样定义可以么?main方法可 ...

  9. Arachnid包含一个简单的HTML剖析器能够分析包含HTML内容的输入流

    Arachnid是一个基于Java的web spider框架.它包含一个简单的HTML剖析器能够分析包含HTML内容的输入流.通过实现Arachnid的子类就能够开发一个简单的Web spiders并 ...

  10. android大牛高焕堂最新力作-android架构师之路

    android大牛高焕堂 个人介绍: Android专家顾问,台湾Android论坛主席,现任亚太地区Android技术大会主席,台湾Android领域框架开发联盟总架构师.发表100多篇Androi ...