URAL 1018 Binary Apple Tree
2017-01-20 18:58
393 查看
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:
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.
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 should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
树形DP~
好久没写,都忘掉怎么写了……忘了循环0的情况改到吐血……
f[i][j]表示以i为根的子树,留下j条边的最大全值和,直接遍历即可~
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 |
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 pointsin 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 |
树形DP~
好久没写,都忘掉怎么写了……忘了循环0的情况改到吐血……
f[i][j]表示以i为根的子树,留下j条边的最大全值和,直接遍历即可~
#include<cstdio> #include<iostream> using namespace std; int n,m,fi[101],ne[201],w[201],v[201],cnt,f[101][101],fa[101],x,y,val; void add(int u,int vv,int val) { w[++cnt]=vv;ne[cnt]=fi[u];fi[u]=cnt;v[cnt]=val; w[++cnt]=u;ne[cnt]=fi[vv];fi[vv]=cnt;v[cnt]=val; } int findd(int u) { int now=0; for(int i=fi[u];i;i=ne[i]) if(w[i]!=fa[u]) { fa[w[i]]=u;now+=findd(w[i])+1; for(int j=min(now,m);j;j--) for(int k=j;k;k--) f[u][j]=max(f[u][j],f[u][j-k]+f[w[i]][k-1]+v[i]); } return now; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&val);add(x,y,val); } findd(1); printf("%d\n",f[1][m]); return 0; }
相关文章推荐
- URAL 1018 Binary Apple Tree(树形dp入门题)
- ural 1018 Binary Apple Tree
- URAL 1018 Binary Apple Tree (树形DP)
- ural 1018 Binary Apple Tree(树dp)
- URAL_1018 Binary Apple Tree 树形DP+背包
- Ural-1018-Binary Apple Tree
- URAL 1018 Binary Apple Tree(树DP)
- Ural 1018 binary apple tree(显性树的树dp)
- ural 1018 Binary Apple Tree(树形dp | 经典)
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
- URAL 1018 Binary Apple Tree (树形DP)
- URAL 1018 binary apple tree
- ural 1018 Binary Apple Tree(树形DP)
- URAL 1018 Binary Apple Tree(树形dp)
- Ural 1018 Binary Apple Tree(树形dp)
- ural 1018 Binary Apple Tree(树形DP)建二叉树
- URAL 1018 Binary Apple Tree 简单树形背包
- 【URAL1018】【树形DP】Binary Apple Tree题解
- URAL 1018 Binary Apple Tree
- URAL1018 Binary Apple Tree(树形DP)