1018. Binary Apple Tree
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
input | output |
---|---|
5 2 1 3 1 1 4 10 2 3 20 3 5 20 |
21 |
Problem Source: Ural State University Internal Contest '99 #2
Difficulty: 307 Printable version Submit solution Discussion (75) My submissions All submissions (15206) All accepted submissions (5087) Solutions rating (3331)
**********************************************************************************************************
思想:先建树,再遍历,分配左右子树的边的数量,合为number;(小菜鸟也是模仿别人)唉唉,
**********************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include<cctype> 7 #include<algorithm> 8 #include<stack> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 int f[1000][1000]; 13 int val[1000][1000]; 14 int visit[1000]; 15 int apple[1000][1000]; 16 int a1,a2,w; 17 int n,number; 18 struct node1 19 { 20 int lc;//左子树根节点 21 int rc;//右子树根结点 22 int s;//权值 23 24 }nod[1000]; 25 void init()//初始化 26 { 27 cin>>n>>number; 28 for(int i=1;i<n;i++) 29 { 30 cin>>a1>>a2>>w; 31 apple[a1][a2]=w; 32 apple[a2][a1]=w; 33 } 34 } 35 void dfs(int root)//建树 把根节点和子树的根节点连起来 36 { 37 visit[root]=1; 38 for(int i=1;i<=n;i++) 39 { 40 if(!visit[i]&&apple[root][i])//判断结点是否访问…,是否相连… 41 { 42 visit[i]=1;//标记 43 if(nod[root].lc==0)//建子树时建右边还是左边(先左后右,左满建右) 44 nod[root].lc=i; 45 else 46 nod[root].rc=i; 47 nod[i].s=apple[root][i]; 48 dfs(i);//扩展树 49 } 50 } 51 } 52 int dfsmax(int root,int num)//从root到叶遍历树,求最值 53 { 54 int i; 55 if(f[root][num]!=-1)return f[root][num];//有值即为最优值返回 56 if(root==0||num==0){f[root][num]=0;return 0;}//超出返回0 57 for(int i=0;i<=num-1;i++) 58 { 59 int ls=dfsmax(nod[root].lc,i);//求左子树要选取边的数量,递归求最优值 60 int rs=dfsmax(nod[root].rc,num-i-1);//求右子树要选取边的数量,递归求最优值 61 if(f[root][num]<ls+rs)f[root][num]=ls+rs;//存最优值, 62 63 } 64 f[root][num]+=nod[root].s;//加根节点所代表的值 65 return f[root][num]; 66 } 67 int main() 68 { 69 memset(apple,0,sizeof(apple));//便于查找有0即为无; 70 init(); 71 memset(visit,0,sizeof(visit));//标记作用 72 memset(f,-1,sizeof(f));//初始化-1,便以识别返回见dfsmax函数 73 dfs(1);//编号root=1; 74 int ans=dfsmax(1,number+1);//由于定义的函数,此处要加1 75 cout<<ans<<endl; 76 return 0; 77 }