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. NextN − 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个点 n-1条边,现在要保留Q条边,求保留下的边的去权值和的最大值。

把边的权值映射到点上,边的权值相当于这个点指向根节点的权值,所以问题转换成对点的操作。

先统计出以当前点为根节点的子树的点数(包括当前根节点),然后dp,

这里dp,以u为根节点保留j个点能得到最大值,状态转移方程

dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v]k]+val)

val是v到u的权值。

/* ***********************************************
Author :guanjun
Created Time :2016/10/15 15:43:48
File Name :timus1018.cpp
************************************************ */
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
struct node{
int y;
int val;
}; vector<node>v[];
int sz[],n,m,num;
int dp[][];
void dfs(int u,int fa){
num++;
sz[u]=;
for(int i=;i<v[u].size();i++){
int y=v[u][i].y;
if(y==fa)continue;
dfs(y,u);
sz[u]+=sz[y];
}
}
void dfs2(int u,int fa){
for(int i=;i<v[u].size();i++){
int y=v[u][i].y;
int val=v[u][i].val;
if(y==fa)continue;
//cout<<u<<" "<<sz[u]<<endl;
dfs2(y,u);
for(int j=sz[u];j>;j--){
for(int k=;k<j;k++){
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[y][k]+val);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
num=;
int x,y,z;
cle(sz);
for(int i=;i<n;i++){
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
cle(dp);
dfs(,-);
dfs2(,-);
cout<<dp[][m+]<<endl;
}
return ;
}

timus 1018. Binary Apple Tree的更多相关文章

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

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

  2. 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 ...

  3. Ural 1018 Binary Apple Tree

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

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

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

  5. BNUOJ 13358 Binary Apple Tree

    Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...

  6. 【URAL 1018】Binary Apple Tree

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

  7. URAL1018 Binary Apple Tree(树形DP)

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

  8. URAL1018 Binary Apple Tree(树dp)

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

  9. URAL1018. Binary Apple Tree

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

随机推荐

  1. 瑞芯微ROCK960 RK3399烧录image后扩容rootfs

    问题描述: RK3399开发板烧录官网提供的ubuntu镜像: Ubuntu 16.04 Server arm64(下载地址:https://www.96boards.org/documentatio ...

  2. 【Codeforces 682C】Alyona and the Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...

  3. 做ssh框架整合的时候,遇到如下错误:AnnotationTransactionAttributeSource is only available on Java 1.5 and higher

    nested exception is java.lang.IllegalStateException: AnnotationTransactionAttributeSource is only av ...

  4. A - 不容易系列之(3)―― LELE的RPG难题 简单递推

    人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即"可乐"),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研 ...

  5. Ubuntu 16.04安装Synaptic Package Manager图形化APT管理工具

    安装: sudo apt-get install synaptic 启动:

  6. Memcached高可用方案收集(集群及分布式)

    Memcached的集群方案有很多,不止magent一个,但是单靠集群软件去实现高可用感觉还是会缺少一步,最推荐的方案应该是软件加编码去实现高可用,至少能保证站点的99.5%的可运行行,以下是集群的方 ...

  7. webpack打包的基础原理-打包后的文件解读

    1.概念 本质上,webpack 基于node平台,利用 node 的各种api来实现 javascript 应用程序的一个静态模块的打包工具. 在打包过程中,构建依赖关系,并且实现模块引用预处理,以 ...

  8. ArcGIS ArcMap “ Add Data” 打开后,一直卡死,无内容

    打开ArcMap能打开,Add Data 或打开mxd就出Runtime Error对话框.打开ArcCatlog或者ArcGlobe出现Runtime Error对话框Runtime Error!P ...

  9. Qos management

    本文基于oracle 11.0.2.3. 主要介绍什么叫Qos management.本文包括以下内容: 什么是 Oracle Database QoS Management? 使用QoS Manag ...

  10. InnoDB: Error: Table &quot;mysql&quot;.&quot;innodb_table_stats&quot; not found.

    1,Mysqldump的时候报错例如以下: 2014-05-05 14:12:37 7f004a9a2700 InnoDB: Error: Table "mysql"." ...