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. Rails 确认params的统一方法

    创建: 2017/11/06    Gemfile  ### デバッグ出力の整形  gem 'awesome_print', :group => [:development, :test]  a ...

  2. bzoj 1593: [Usaco2008 Feb]Hotel 旅馆【线段树】

    参考:https://blog.csdn.net/u010336344/article/details/53034372 神一样的线段树 线段树上维护:ll从左开始最长空段:rr从右开始最长空段:le ...

  3. Ubuntu 16.04 安装OpenSSH7.4

      前几天突然收到接到网安总队下发通知说我们在aws里面的服务器存在重大漏洞及安全隐患.必须在规定时间内修改.我们收到邮件打开Excel发现这些问题 是由于OpenSSH版本太低导致的.于是便安排紧急 ...

  4. GIt学习之路 第二天 创建版本库

    本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 创建版本库 阅读: 1859216 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文 ...

  5. Android 性能优化(27)*zipalign让apk数据对齐,运行更快。

    1.zipalign 简介 zipalign is an archive alignment tool that provides important optimization to Android ...

  6. [转]oracle 同义词 synonym

    本文转自:http://blog.csdn.net/generalfu/article/details/7906561 同义词定义 当一个用户想访问另外一个用户的表时, 需要在表前加用户名,总加表名不 ...

  7. 总结MySQL中SQL语法的使用

    --where子句操作符: where子句操作符 = 等于 <> 不等于(标准语法) != 不等于(非标准语法,可移植性差) < 小于 <= 小于等于 > 大于 > ...

  8. java用匿名内部类实现多线程堆内存变量共享

    匿名内部类介绍:http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html 用Runnable模拟实现共享堆内存变量 import ...

  9. duilib属性

    原文转载自:http://blog.csdn.net/lixiang987654321/article/details/45008441 这里我想讲解一下duilib中的一些属性的理解,当然这是一篇永 ...

  10. [ SPOJ Qtree1 ] Query on a tree

    \(\\\) Description 给定 \(n\) 个点的树,边按输入顺序编号为\(1,2,...n-1\) . 现要求按顺序执行以下操作(共 \(m\) 次): \(CHANGE\ i\ t_i ...