ural 1018 Binary Apple Tree 树形dp

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

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

 

 

题意:给你一棵二叉树,根节点是1,除根节点1外,其他每个点都有一个对应的值。

        n个节点,要求让你保留m条边,最大的价值。当然,有依赖的关系。

思路:保留是边,这个和其他题目,保留多少个点,就有区别了。

     刚开始,题意都写错,第一种测试数据就错了。囧...

        如何处理呢?  由于不知道,输入顺序中,谁是父亲节点,双向图+use[]标记就可以建树了。

        状态转移也好写。

        dp[ k ] [ j ]  以k为根节点,占用 j 个点获得的最大值。

        dp[ k ] [ j ]= max(  dp[ k ] [ j ],   dp[ k ] [ j -s ] + dp [ t ] [ s ]  );

        那么就ok了么

        不是的,题目意思是边,那你怎么办? 由于有依赖关系的存在,不可能存在单独的独立的几条线段,而是连接起来的

所以是m+1个节点。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 
 7 int n,m;
 8 struct node
 9 {
10     int next[105];
11     int val[105];
12     int num;
13 }f[105];
14 int dp[105][105];
15 bool use[105];
16 
17 int Max(int x,int y)
18 {
19     return x>y? x:y;
20 }
21 
22 void dfs(int k)
23 {
24     int i,j,t,s;
25     use[k]=true;
26     for(i=1;i<=f[k].num;i++)
27     {
28         t=f[k].next[i];
29         if(use[t]==true)continue;
30 
31         dp[t][1]=f[k].val[i];
32         dfs(t);
33         for(j=m;j>=1;j--)
34         {
35             for(s=1;s<=j;s++)
36             {
37                 if(dp[k][j-s]!=-1)
38                 dp[k][j]=Max(dp[k][j],dp[k][j-s]+dp[t][s]);
39             }
40         }
41     }
42 }
43 int main()
44 {
45     int i,x,y,z;
46     while(scanf("%d%d",&n,&m)>0)
47     {
48         memset(dp,-1,sizeof(dp));
49         memset(use,false,sizeof(use));
50         for(i=0;i<=100;i++) f[i].num=0;
51 
52         for(i=1;i<n;i++)
53         {
54             scanf("%d%d%d",&x,&y,&z);
55             f[x].num++;
56             f[x].next[f[x].num]=y;
57             f[x].val[f[x].num]=z;
58 
59             f[y].num++;
60             f[y].next[f[y].num]=x;
61             f[y].val[f[y].num]=z;
62         }
63         dp[1][1]=0;
64         m++;
65         dfs(1);
66         printf("%d\n",dp[1][m]);
67     }
68     return 0;
69 }

 

 

posted @ 2013-10-30 20:03  芷水  阅读(225)  评论(0编辑  收藏  举报