【URAL 1018】Binary Apple Tree
http://vjudge.net/problem/17662
loli蜜汁(面向高一)树形dp水题
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct nodeTreeDP {
struct node {int nxt, to, w;} E[203];
int n, q, cnt, point[103], apple[103], left[103], right[103], f[103][103], size[103];
nodeTreeDP() {
cnt = 0;
memset(E, 0, sizeof(E));
memset(f, 0, sizeof(f));
memset(left, 0, sizeof(left));
memset(right, 0, sizeof(right));
memset(point, 0, sizeof(point));
memset(apple, 0, sizeof(apple));
}
void ins(int u, int v, int w) {E[++cnt] = (node) {point[u], v, w}; point[u] = cnt;}
void dfs(int x, int fa) {
if (x == 0) return;
for(int i = point[x]; i; i = E[i].nxt)
if (E[i].to != fa) {
apple[E[i].to] = E[i].w;
if (!left[x]) left[x] = E[i].to;
else right[left[x]] = E[i].to;
}
dfs(right[x], fa); dfs(left[x], x);
size[x] = size[left[x]] + size[right[x]] + 1;
}
void TreeDP(int x) {
if (x == 0) return;
TreeDP(right[x]); TreeDP(left[x]);
int tot = size[x], rightnum;
for(int top = 0; top <= tot; ++top) {
f[x][top] = max(f[x][top], f[right[x]][top]);
for(int leftnum = 1; leftnum <= top; ++leftnum) {
rightnum = top - leftnum;
f[x][top] = max(f[x][top], f[left[x]][leftnum - 1] + apple[x] + f[right[x]][rightnum]);
}
}
}
void ansit() {
TreeDP(left[1]);
printf("%d\n", f[left[1]][q]);
}
} *T;
int main() {
T = new nodeTreeDP;
scanf("%d%d", &T->n, &T->q);
int u, v, w;
for(int i = 1; i < T->n; ++i) {
scanf("%d%d%d", &u, &v, &w);
T->ins(u, v, w);
T->ins(v, u, w);
}
T->dfs(1, 0);
T->ansit();
return 0;
}
【URAL 1018】Binary Apple Tree的更多相关文章
- 【LeetCode 173】Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- URAL 1018 Binary Apple Tree(树DP)
Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【BZOJ 1018】 [SHOI2008]堵塞的交通traffic
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 [题意] [题解] 按照这里的题解写的http://blog.csdn.net/ ...
- 【BZOJ 4353】 Play with tree
[题目链接] 点击打开链接 [算法] 树链剖分 对于线段树的每个节点,记录这段区间的最小值,最小值的个数,值为0的个数,此外,还要维护两个懒惰标记 [代码] 本题细节很多,写程序时要认真严谨! #in ...
随机推荐
- 【一周读书】All life is problem solving
书籍:<开放的智力> 采铜是我在知乎关注最早的大V之一,那时我脑里有一大堆疑惑和问题,是他的答案帮助我理清了思绪.我从他身上学习到对书籍的爱好,对思维方法的关注,对智慧的向往.读这本小集子 ...
- 《Invert》开发日志03:一些想法
本来标题想写“详细设计”,但是由于独立游戏开发有很强的探索性,最终项目一定是经过原型调整迭代而来的,所以在实际效果出来之前把设计做得太细并没有太大意义,现在只能先陈列目前的一些想法,不能定义“它是什么 ...
- 网格测地线算法(Geodesics in Heat)附源码
测地线又称为大地线,可以定义为空间曲面上两点的局部最短路径.测地线具有广泛的应用,例如在工业上测地线最短的性质就意味着最优最省,在航海和航空中,轮船和飞机的运行路线就是测地线.[Crane et al ...
- Vijos1019 补丁VS错误[最短路 状态压缩]
描述 错误就是人们所说的Bug.用户在使用软件时总是希望其错误越少越好,最好是没有错误的.但是推出一个没有错误的软件几乎不可能,所以很多软件公司都在疯狂地发放补丁(有时这种补丁甚至是收费的).T公 ...
- 06章 Struts2国际化
1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素.换言之,应用程序的功 ...
- 在Flex4中嵌入字体
如果要使用的字体不是系统字体,可以把字体嵌入到Flash中,然后引用该字体.不过字体文件一般都比较大,慎重使用该功能. 官方例子 http://help.adobe.com/en_US/flex/us ...
- 入门训练 Fibonacci数列
入门训练 Fibonacci数列 时间限制:1.0s 内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...
- Eclipse仿Visual AssistX 编辑着色插件
1. 风格下载 http://www.eclipsecolorthemes.org/?view=theme&id=4602 2. 风格的安装与卸载 http://www.cnblogs.com ...
- php碎片
1.flock LOCK_EX 独占锁定 LOCK_SH 共享锁定 LOCK_UN 解除锁定 LOCK_NB 锁定但不堵塞进程,直接返回false 2. fseek SEEK_CUR SEEK_END ...
- PAT 1025. 反转链表 (25)
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5→6,即最后 ...