• URAL1018 Binary Apple Tree


    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

     Status

    Description

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

    inputoutput
    5 2
    1 3 1
    1 4 10
    2 3 20
    3 5 20
    
    21
    

    Source

    Problem Source: Ural State University Internal Contest '99 #2 
     
     
    树形DP
    f[当前结点][所选结点数]=最优解
     
    dp的时候num要多加1,是因为当前结点也得选,才能选子结点。
     
     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mxn=200;
     9 struct edge{
    10     int v,w;
    11     int nxt;
    12 }e[mxn];
    13 int hd[mxn],mct=0;
    14 void add_edge(int u,int v,int dis){
    15     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=dis;hd[u]=mct;
    16     return;
    17 }
    18 int f[mxn][mxn];
    19 int num[mxn];
    20 int n,m;
    21 void dp(int u,int fa){
    22     int i,j,k;
    23     num[u]=0;
    24     for(i=hd[u];i;i=e[i].nxt){
    25         int v=e[i].v;
    26         if(v==fa)continue;
    27         dp(v,u);
    28         num[u]+=num[v]+1;
    29         for(j=num[u];j;--j){
    30             for(k=j;k;k--){
    31                 f[u][j]=max(f[u][j],f[u][j-k]+f[v][k-1]+e[i].w);
    32             }
    33         }
    34     }
    35     return;
    36 }
    37 int main(){
    38     scanf("%d%d",&n,&m);
    39     int i,j;
    40     int u,v,d;
    41     for(i=1;i<n;i++){
    42         scanf("%d%d%d",&u,&v,&d);
    43         add_edge(u,v,d);
    44         add_edge(v,u,d);
    45     }
    46     dp(1,0);
    47     printf("%d
    ",f[1][m]);
    48     return 0;
    49 }
  • 相关阅读:
    mysql 全量备份和增量备份
    mysql 修改密码提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    MHA 常见问题解决
    MHA 数据库高可用+ GTID 同步测试部署
    通过电脑抓手机端log
    用js递归遍历树结构
    js实现全屏
    使表格随着内容自适应宽度
    POST请求
    vue中组件通信
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5918359.html
Copyright © 2020-2023  润新知