ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【URAL1018】Binary Apple Tree

2020-08-11 18:31:47  阅读:309  来源: 互联网

标签:Binary branches Apple int tree Tree len apples dp


题目链接

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. ### 输入格式 First line of input contains two numbers: $N$ and $Q$ ($2 \le N \le 100$;$1 \le Q \le 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 should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-) ### 样例输入
5 2
1 3 1
1 4 10
2 3 20
3 5 20
### 样例输出
21
## 题解 题意:有一棵树,每条边都有一个权值,求包含根的有$Q$条边的联通块的权值之和最大。 那么我们考虑树上dp $dp[i][j]$表示以$i$为根,保留$j$条边最大的权值之和。 那么转移方程如下: $dp[u][i]=max$($dp[u][i],dp[u][i-k-1]+dp[son][k]$) $dp[u][0]=val[u][fa]$ 记得把$dp[u][0]$的值初始化为$u$和它父亲的边的权值,如果$u$不是根结点。 上代码: ```cpp #include using namespace std; int n,q; int u,v,w; struct aa{ int to,nxt,v; }p[209]; int h[109],len=1; void add(int u,int v,int w){ p[++len].to=v; p[len].v=w; p[len].nxt=h[u]; h[u]=len; } int ss[109]; int dp[109][109]; void dfs(int u,int fa,int us){ ss[u]=0; for(int j=h[u];j;j=p[j].nxt){ if(p[j].to==fa) continue; dfs(p[j].to,u,p[j].v); ss[u]+=1+ss[p[j].to];//记录以u为根的子树的节点数 } dp[u][0]=us;//别忘了u节点和父亲的边 for(int j=h[u];j;j=p[j].nxt){ if(p[j].to==fa) continue; for(int i=q;i>=1;i--){//从大到小枚举防止重复计算 for(int o=0;i-o-1>=0 && o<=ss[p[j].to];o++){ dp[u][i]=max(dp[u][i],dp[u][i-o-1]+dp[p[j].to][o]); } } } } int main(){ scanf("%d%d",&n,&q); for(int j=1;j<n;j++){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } dfs(1,0,0); printf("%d",dp[1][q]); return 0; } ```

标签:Binary,branches,Apple,int,tree,Tree,len,apples,dp
来源: https://www.cnblogs.com/linjiale/p/13478194.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有