现在的位置: 首页 > 综合 > 正文

Binary Apple Tree

2011年12月24日 ⁄ 综合 ⁄ 共 2937字 ⁄ 字号 评论关闭

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;
}

 

 

抱歉!评论已关闭.