zoukankan      html  css  js  c++  java
  • A

    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. 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 ;-)

    Example

    inputoutput
    5 2
    1 3 1
    1 4 10
    2 3 20
    3 5 20
    
    21
    #include<bits/stdc++.h>
    using namespace std;
    vector<int>g[105];
    int num[105][105],cnt[105];
    int dp[105][105];
    int n,q;
    bool vis[105];
    
    void getcnt(int u)
    {
        vis[u]=true;
        cnt[u]=0;
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                cnt[u]++;
                getcnt(v);
                cnt[u]+=cnt[v];
            }
        }
    }
    
    void dfs(int u)
    {
        vis[u]=true;
        int first=0,second=0;
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                if(first)
                  second=v;
                else
                  first=v;
                dfs(v);
            }
        }
        if(second)
        {
            for(int i=1;i<n;i++)
            {
                if(i>cnt[u])
                {
                    dp[u][i]=-1;
                    continue;
                }
                dp[u][i]=0;
                if(cnt[first]>=i-1)
                {
                    dp[u][i]=max(dp[u][i],num[u][first]+dp[first][i-1]);
                }
                if(cnt[second]>=i-1)
                {
                    dp[u][i]=max(dp[u][i],num[u][second]+dp[second][i-1]);
                }
                int tmp=num[u][first]+num[u][second];
                for(int j=0;j<=i-2;j++)
                {
                    if(cnt[first]>=j&&cnt[second]>=i-2-j)
                      dp[u][i]=max(dp[u][i],tmp+dp[first][j]+dp[second][i-2-j]);
                      
                }
            }
        }
        else if(first)
        {
            for(int i=1;i<n;i++)
            {
                if(i>cnt[u])
                {
                    dp[u][i]=-1;
                    continue;
                }
                if(cnt[first]>=i-1)
                {
                    dp[u][i]=num[u][first]+dp[first][i-1];
                }
            }
        }
        else
        {
            for(int i=1;i<n;i++)
              dp[u][i]=-1;
        }
    }
    
    int main()
    {
        cin>>n>>q;
        for(int i=1;i<n;i++)
        {
            int u,v,c;
            cin>>u>>v>>c;
            g[u].push_back(v);
            g[v].push_back(u);
            num[u][v]=num[v][u]=c;
        }
        memset(vis,false,sizeof(vis));
        getcnt(1);
        memset(vis,false,sizeof(vis));
        dfs(1);
        cout<<dp[1][q]<<"
    ";
        return 0;
    }
  • 相关阅读:
    Decision Tree
    Bigtable:结构化数据的分布式存储系统
    堆排序分析及php实现
    PDO防sql注入原理分析
    memcached的key,value,过期时间的限制
    归并排序--详细解释版
    深入理解redis持久化
    strace追踪未开始或者来不及捕获pid的进程(译)
    php中session锁--如何防止阻塞请求(译)
    php多进程总结
  • 原文地址:https://www.cnblogs.com/ylrwj/p/11960157.html
Copyright © 2011-2022 走看看