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. CSS 如何让li横向居中显示

    先给一个简单的示例HTML代码 <body> <form id="form1" runat="server"> <div id=& ...

  2. 使用python获得N个区分度较高的RGB颜色值

    获得任意N个区分度最高的RGB颜色值是一个经典的问题,之前在做一些可视化的东西时需要解决这个问题.首先去网上找了一些方法,未果,于是想自己来搞,心里的想法是,先给出一个距离函数用来度量两个RGB颜色值 ...

  3. ZOJ - 3981 - Balloon Robot (思维)

    参考自:https://blog.csdn.net/qq_36553623/article/details/78445558 题意: 第一行三个数字n, m, q表示有m个座位围成一个环,n个队伍,q ...

  4. Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)

    题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...

  5. <Zookeeper>入门 概念

    分布式协调服务 Zookeeper 分布式环境的特点 1.分布性 基于一个硬件或者多个硬件设备以及多个软件组成的分布在不同网络计算机上的系统架构,通过消息传递进行通信协调.在空间上部署是可以任意的,网 ...

  6. 洛谷 2434 [SDOI2005]区间

    [题解] 鲜活的大水题... 把区间排个序然后瞎搞就可以了,发现现在区间的左端点比之前区间的最大的右端点还大,那就增加一个答案区间.每次更新目前最大右区间. #include<cstdio> ...

  7. Ural 1114 Boxes

    Boxes Time Limit: 600ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 1114 ...

  8. Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度

     Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度 Android StatusBarUtil是github上的一个开源项目,主页:https://githu ...

  9. mybatis源码阅读-执行一个sql的流程(九)

    图解 图片来源:https://my.oschina.net/zudajun/blog/670373 Mapper接口调用原理 我们整合成Spring  直接使用Mapper就能执行对应的sql 表现 ...

  10. Python学习笔记 (2)变量、常量和数据类型

    变量 顾名思义,变量就是一个会变的量,用一个变量名表示,指向内存中一片区域,而指向的区域存的是什么,这个变量就是什么数据类型,和C/C++挺不一样的.变量数据类型可以通过赋值变来变去(这就叫动态语言, ...