ural 1018 Binary Apple Tree(树形DP)
2016-05-25 16:37
513 查看
1018. Binary Apple Tree
Time limit: 1.0 secondMemory 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 |
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 andQ (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 | output |
---|---|
5 2 1 3 1 1 4 10 2 3 20 3 5 20 | 21 |
有一棵苹果树,苹果树的是一棵二叉树,共N个节点,树节点编号为1~N,编号为1的节点为树根,边可理解为树的分枝,每个分支都长着若干个苹果,现在要要求减去若干个分支,保留M个分支,要求这M个分支的苹果数量最多。
输入:
N M
接下来的N-1行是树的边,和该边的苹果数N and M (1 ≤ M < N;1 < N ≤ 100)
输出:
剩余苹果的最大数量。
思路:由于是一颗二叉树,所以可以用树形DP,对于每一点u寻找u的子树,定义dp[u][j]表示在以u为根的子树保留j个分支可以得到的最大苹果数量
最后答案输出dp[1][m]即可
#include <stdio.h> #include <string.h> #define max(a,b) (a)>(b)?(a):(b) #define min(a,b) (a)<(b)?(a):(b) int N, Q; struct node { int v; int w; } edge[205]; int first[105]; int next[205]; int size; int dp[105][105]; void add(int u, int v, int w) { edge[size].v = v; edge[size].w = w; next[size] = first[u]; first[u] = size++; } int dfs(int u, int pre) { int sum = 0; for(int i = first[u]; i != -1; i = next[i]) { int v = edge[i].v; if(v == pre) continue; int w = edge[i].w; sum += dfs(v, u) + 1; for(int j = min(sum, Q); j >= 1; --j) { for(int k = j; k >= 1; --k) { dp[u][j] = max(dp[u][j], dp[u][j-k] + dp[v][k-1] + w); } } } return sum; } int main() { int u, v, w; while(scanf("%d%d", &N, &Q) != EOF) { memset(first, -1, sizeof(first)); size = 0; for(int i = 1; i < N; i++) { scanf("%d%d%d", &u, &v, &w); add(u, v, w); add(v, u, w); } memset(dp, 0, sizeof(dp)); dfs(1, -1); printf("%d\n", dp[1][Q]); } return 0; }
相关文章推荐
- android listview与adapter用法
- 详解Android TabHost的多种实现方法 附源码下载
- Android 自定义View 让内容跟随手指滚动
- Android的拍照功能
- Android端 WebP图片压缩与传输的一点探索
- Android设计中如何切图.9.png(点9图)
- android KTV 开发(一)自定义videoView 实现视频大小自由缩放、全屏与预览
- APP被kill后重新激活的几种方案
- android刷新媒体库
- swift资源库——开发利器 Playground
- android二维码扫描、读取、识别、生成、带logo
- iostat命令——用于输出CPU和磁盘I/O相关的统计信息
- uva,132 Bumpy Objects (凸包,角度)
- Android中Style和Theme的使用
- android 处理程序全局异常和错误,UncaughtExceptionHandler
- Android Studio自动检查代码规范并提示如何优化的一些插件
- Android中修改默认字体的大小----fontScale
- android listview判断上滑还是下滑
- 打造浪漫的Android表白程序
- Android静默安装实现方案,秒装(测试没有通过)和智能安装(实现)功能