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. Unity使用外部版本控制

    Using External Version Control Systems with Unity Unity offers an Asset Server add-on product for ea ...

  2. bzoj 2730: [HNOI2012]矿场搭建【tarjan】

    先tarjan找割点和点双连通分量,然后对一个点双,如果没有割点,那么需要建立两个出口(割掉一个另一个备用):如果只有一个割点,出口可以设立在任意一个非割点的地方:如果有两个及以上个割点,就不用建出口 ...

  3. bzoj 1707: [Usaco2007 Nov]tanning分配防晒霜【贪心||最大流(?)】

    洛谷上能过的最大流bzoj上T了--但是贪心做法明明在洛谷上比最大流要慢啊--如果是最大流的话就是裸题了吧 说一下贪心,就按照防晒霜排序,然后对每一个防晒霜选一头可以使用的且r最小的牛 就,没了. 贪 ...

  4. Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域

    //从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...

  5. GIT学习之路第一天 简介及其安装

    本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 那什么是版本控制 ...

  6. 全面学习ORACLE Scheduler特性(10)管理Chains

    5.2  管理Chains 5.2.1  修改Chains属性 基本上碰到修改CHAIN属性的机率不会太大,因此确实没啥可修改的,对于CHAIN对象来说,能够修改的属性只有两个:evaluation_ ...

  7. java 配置信息类 Properties 的简单使用

    Properties :(配置信息类) 是一个表示持久性的集合 ,继承 Hashtable ,存值是以键-值得方式  主要用于生产配置文件和读取配置文件信息. 简单的实例: import java.i ...

  8. 2199. [HZOI 2016] 活动投票

    ★★   输入文件:hztp.in   输出文件:hztp.out   简单对比 时间限制:0.5 s   内存限制:2 MB [题目描述] 衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知 ...

  9. 实例化Class类的5种方式

    Java的数据类型可以分为两类,即引用类型和原始类型.对于每种类型的对象,Java虚拟机会实例化不可变的java.lang. Class对象.它提供了在运行时检查对象属性的方法,这些属性包括它的成员和 ...

  10. JavaScript开发心得--如何传递某行数据给下一页

    1, 应用场景 在某个html页面显示一批数据,如20个用户的名称.年龄等,每行都要一个编辑按钮,点击编辑后,将此行数据带入某个专门的编辑页进行显示,修改后保存. 问题是 点击编辑按钮后,如何得知要编 ...