Binary Apple Tree

Time Limit: 1000ms
Memory Limit: 16384KB

This problem will be judged on Ural. Original ID: 1018
64-bit integer IO format: %lld      Java class name: (Any)

 
 
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 Nis 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. Qdenotes 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

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

Sample Output

21

Source

 
解题:树形dp,各种dp各种凌乱。dp[u][i]表示标号为u的根,保留i个条树枝。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
struct arc {
int to,w;
arc(int x = ,int y = ):to(x),w(y) {}
};
vector<arc>g[];
int n,m,dp[][],cnt[];
void dfs(int u,int fa) {
cnt[u] = ;
for(int i = ; i < g[u].size(); i++) {
if(g[u][i].to == fa) continue;
dfs(g[u][i].to,u);
cnt[u] += cnt[g[u][i].to];
}
for(int i = ; i < g[u].size(); i++) {
if(g[u][i].to == fa) continue;
for(int j = cnt[u]; j > ; j--) {
for(int k = ; k <= cnt[g[u][i].to] && k < j; k++) {
dp[u][j] = max(dp[u][j],dp[u][j-k]+dp[g[u][i].to][k]+g[u][i].w);
}
}
}
}
int main() {
int i,u,v,w;
while(~scanf("%d %d",&n,&m)) {
for(i = ; i <= n; i++)
g[i].clear();
for(i = ; i < n; i++) {
scanf("%d %d %d",&u,&v,&w);
g[u].push_back(arc(v,w));
g[v].push_back(arc(u,w));
}
memset(dp,,sizeof(dp));
dfs(,-);
printf("%d\n",dp[][m+]);
}
return ;
}

BNUOJ 13358 Binary Apple Tree的更多相关文章

  1. 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 bina ...

  2. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  3. timus 1018. Binary Apple Tree

    1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...

  4. 【URAL 1018】Binary Apple Tree

    http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...

  5. Ural 1018 Binary Apple Tree

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在 ...

  6. URAL1018 Binary Apple Tree(树形DP)

    题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...

  7. URAL1018 Binary Apple Tree(树dp)

    组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...

  8. URAL1018. Binary Apple Tree

    链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...

  9. Ural-1018 Binary Apple Tree(树形dp+分组背包)

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

随机推荐

  1. 解决Error for wireless request "Set Mode" (8B06) 问题 (转载)

    转自:http://blog.csdn.net/muge0913/article/details/17062871 在运行以下命令的时候,意外的出错,最后google了下,最终才确定了原因,因为在运行 ...

  2. SQL Server 添加描述

    添加描述的格式 exec sys.sp_addextendedproperty @name = N'MS_Description' ,@value = 'value',@level0type=N'SC ...

  3. Book-MySQL-Operate

    创建数据库 CREATE DATABASE db_name 查看数据库 SHOW DATABASES 选择数据库 USE db_name 删除数据库 DROP DATABASE db_name 列主键 ...

  4. Spring中AOP的两种代理方式(Java动态代理和CGLIB代理-转载

    内容是摘抄的,不知最初的原作者,见谅 Java 动态代理.具体有如下四步骤: 通过实现 InvocationHandler 接口创建自己的调用处理器: 通过为 Proxy 类指定 ClassLoade ...

  5. bzoj 1599: [Usaco2008 Oct]笨重的石子【枚举】

    --我为什么要写这种题解-- 枚举投掷情况即可 #include<iostream> #include<cstdio> using namespace std; int s1, ...

  6. C - GCD LCM

    Description The GCD of two positive integers is the largest integer that divides both the integers w ...

  7. JSP/Servlet Web应用中.properties文件的放置与读取

    本地项目 在本地类库中,我经常使用当前目录来放置.properties文件,这时调用方只要引用我的jar,并且将我的.properties放在他的classpath里面即可,比如: p.load(ne ...

  8. sql 获取当前季度期间

    select year(getdate())*10000+((month(getdate())/3)*3+1)*100 + 1 --季度第一天 select year(getdate())*10000 ...

  9. SQL数据库基础知识——抽象类

    抽象类,只为继承而出现,不定义具体的内容,只规定该有哪些东西:一般抽象类中只放置抽象方法,只规定了返回类型和参数:比如: 人 - 有吃饭,睡觉方法: 男人 - 继承人抽象类,必须实现吃饭,睡觉的方法主 ...

  10. 简单的win7-cmd命令提示符

    在win7打开cmd窗口 有两个路径:(1)开始 -->所有程序 --> 附件 --> 命令提示 (2)开始 -->在搜索框输入 “cmd”   指令 作用 对文件夹的操作   ...