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:
  1. 2 5
  2. \ /
  3. 3 4
  4. \ /
  5. 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
  1. 5 2
  2. 1 3 1
  3. 1 4 10
  4. 2 3 20
  5. 3 5 20
  1. 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的权值。

  1. /* ***********************************************
  2. Author :guanjun
  3. Created Time :2016/10/15 15:43:48
  4. File Name :timus1018.cpp
  5. ************************************************ */
  6. #include <bits/stdc++.h>
  7. #define ull unsigned long long
  8. #define ll long long
  9. #define mod 90001
  10. #define INF 0x3f3f3f3f
  11. #define maxn 10010
  12. #define cle(a) memset(a,0,sizeof(a))
  13. const ull inf = 1LL << ;
  14. const double eps=1e-;
  15. using namespace std;
  16. priority_queue<int,vector<int>,greater<int> >pq;
  17. struct Node{
  18. int x,y;
  19. };
  20. struct cmp{
  21. bool operator()(Node a,Node b){
  22. if(a.x==b.x) return a.y> b.y;
  23. return a.x>b.x;
  24. }
  25. };
  26.  
  27. bool cmp(int a,int b){
  28. return a>b;
  29. }
  30. struct node{
  31. int y;
  32. int val;
  33. };
  34.  
  35. vector<node>v[];
  36. int sz[],n,m,num;
  37. int dp[][];
  38. void dfs(int u,int fa){
  39. num++;
  40. sz[u]=;
  41. for(int i=;i<v[u].size();i++){
  42. int y=v[u][i].y;
  43. if(y==fa)continue;
  44. dfs(y,u);
  45. sz[u]+=sz[y];
  46. }
  47. }
  48. void dfs2(int u,int fa){
  49. for(int i=;i<v[u].size();i++){
  50. int y=v[u][i].y;
  51. int val=v[u][i].val;
  52. if(y==fa)continue;
  53. //cout<<u<<" "<<sz[u]<<endl;
  54. dfs2(y,u);
  55. for(int j=sz[u];j>;j--){
  56. for(int k=;k<j;k++){
  57. dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[y][k]+val);
  58. }
  59. }
  60. }
  61. }
  62. int main()
  63. {
  64. #ifndef ONLINE_JUDGE
  65. freopen("in.txt","r",stdin);
  66. #endif
  67. //freopen("out.txt","w",stdout);
  68. while(cin>>n>>m){
  69. num=;
  70. int x,y,z;
  71. cle(sz);
  72. for(int i=;i<n;i++){
  73. cin>>x>>y>>z;
  74. v[x].push_back({y,z});
  75. v[y].push_back({x,z});
  76. }
  77. cle(dp);
  78. dfs(,-);
  79. dfs2(,-);
  80. cout<<dp[][m+]<<endl;
  81. }
  82. return ;
  83. }

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. CAD制作简单动画

    主要用到函数说明: IMxDrawEntity::Rotate 旋转一个对象.详细说明如下: 参数 说明 [in] IMxDrawPoint* basePoint 旋转基点 [in] DOUBLE d ...

  2. docker搭建日志收集系统EFK

    EFK Elasticsearch是一个数据搜索引擎和分布式NoSQL数据库的组合,提过日志的存储和搜索功能. Fluentd是一个消息采集,转化,转发工具,目的是提供中心化的日志服务. Kibana ...

  3. 魂酥的NOIP2018(真实)游记

    NOIP之后才开博客 作为一个高一零基础蒟蒻 想说什么似乎也没什么可说的 才学几个月似乎也没什么发言权就是了 Day -1 期中考爆0,似乎是班里学OI的考得最惨的一个 岂不美哉 要么我也没想考好 也 ...

  4. HTTP服务和APACHE

    HTTP服务和APACHE 1. 跨Internet的主机间通讯 要通过Internet进行通信,至少需要一对套接字:其中一个运行在客户端,定义了一个唯一的客户进程,称之为ClientSocket,另 ...

  5. 3.3.3 char 类型

        char类型原本用于表示单个字符.不过,现在情况已经有所变化.如今,有些Unicode字符可以用一个char值描述,另外一些Unicode字符则需要两个 char 值.       char类 ...

  6. 用记事本写第一个Java程序

    public class Welcome{ public static void main(String[] args){ System.out.println("我是尚学堂的高淇,很高兴认 ...

  7. 【12】AngularJS 事件

    AngularJS 事件 AngularJS 有自己的 HTML 事件指令. ng-click 指令 ng-click 指令定义了 AngularJS 点击事件. <div ng-app=&qu ...

  8. Leetcode 80.删除重复数组的重复项

    删除重复数组的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...

  9. HDU 2082 母函数法

    #include <cstdio> #include <cstring> using namespace std; ] , dp[][]; int main() { // fr ...

  10. VScode输出中文乱码的解决方法------测试过可以用

    用python写个爬虫,配置个VScode环境,发现输出都是乱码,翻阅网站后发现一个简单有效的方法,在此谢过网络上的大牛们的无私分享,我也在此记录一下,以备后用: 文件---->首选项----& ...