Binary Apple Tree

來源:互聯網
上載者:User

from timus:

   

Time Limit: 1.0 second
Memory Limit: 16 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. 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
5 2
1 3 1
1 4 10
2 3 20
3 5 20

output
21

 

 

動態規劃。。。

為方便處理,自己根據輸入用數組建立一棵樹,以表示好各個節點的關係。

 

#include<iostream>#include<fstream>#include<assert.h>using namespace std;#define MAXV (106)struct node{    int key;    int parent;    int w;//weight.    int lc,rc;//left child,right child.    node(int k = -1,int p = -1):key(k),parent(p),lc(-1),rc(-1),w(0){}};static node tree[MAXV];static int  dp[MAXV][MAXV];int appTree(int index,int e,int br);static void SetupTree(istream& fin){    int num,br;//number of nodes,branch.    fin >> num >> br;    tree[1].parent = 1;//root    int i = 0;    while(i++ < num -1)    {        int s,e,v;        fin >> s >> e >>v;        if(tree[s].parent != -1 && tree[e].parent == -1)        {           tree[e].parent = s;           tree[e].w = v;           if(tree[s].lc == -1)              tree[s].lc = e;           else if(tree[s].rc == -1)               tree[s].rc = e;           else               assert(0);        }        else if(tree[s].parent == -1 && tree[e].parent != -1)        {            tree[s].parent = e;            tree[s].w = v;            if(tree[e].lc == -1)                tree[e].lc = s;            else if(tree[e].rc == -1)                tree[e].rc = s;            else                assert(0);        }        else        {            assert(0);        }    }    int res = appTree(1,num,br);    cout << res << endl;}static int appTree(int index,int e,int br){    if(br < 0 || index > e || index == -1)        return 0;    if(br == 0 || (tree[index].rc == -1 && tree[index].rc == -1))        return tree[index].w;    if(dp[index][br] != -1)        return dp[index][br];    int maxv = 0;    for(int i = -1;i <= br - 1;++i)    {        int lw = 0,rw = 0;        if(i >= 0)        {            int lef = tree[index].lc;            if(lef != -1)            {                lw = appTree(lef,e,i);            }        }        if(br - 1 - (i+1) >= 0)        {            int rig = tree[index].rc;            if(rig != -1)            {              rw = appTree(rig,e,br - 1 - (i+1));            }        }        if(lw + rw > maxv)            maxv = lw + rw;    }    maxv += tree[index].w;    dp[index][br] = maxv;    return maxv;}static int main(){    //ifstream fin("appTree.dat");    for(int i = 0;i < MAXV;i++)    {        for(int j = 0;j < MAXV;++j)            dp[i][j] = -1;    }    SetupTree(cin);    return 0;}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.