【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 ...
随机推荐
- save()、saveOrUpdate()、merge()的区别
一.Save() save()方法能够保存实体到数据库.假如两个实体之间有关系(例如employee表和address表有一对一关系),如果在没有事务的情况下调用这个方法保存employee这个实体, ...
- C# Enum 进行逻辑运算
Enum定义 enum 全称(Enumeration),即一种由一组称为枚举数列表的命名常量组成的独特类型. 通常情况下,最好是在命名空间內直接定义 enum,以便该命名空间中所有的类都能够同样方便地 ...
- 实现PD控制
尝试为场加入PD控制 在之前的模拟中,需要最小化一个能量函数H. 这样做的原因是,由理想约束的特性(约束反力垂直于虚位移),对于不含体积蒙皮的情况,可以推出 对于表面点,有 J^T * Σfs = 0 ...
- js获取可视区大小和页面大小的兼容性写法
var getPageSize = function() { var scrW, scrH; if(window.innerHeight && window.scrollMaxY) { ...
- Python的高级特性10:无聊的@property
@property装饰器其实有点无聊,单独拿出来作为一个知识点其实没必要,尽管它可以将方法变成属性,让get和set方法更好用,但是,它破坏了python的简洁(不是代码的简洁而是指语法上). 下面来 ...
- Python的高级特性4:函数式编程
函数式编程的核心就是把函数当成对象来进行编程. 有两个常用到的方法:map/reduce,filter,其中map和filter是内建方法,而reduce不是,所以需要import相关模块. map接 ...
- ES6 WeakSet数据结构 与Set十分相似
它与Set十分相似,对象的值也不能是重复的,与Set不同点: .WeakSet成员只能够是对象. .作为WeakSet成员的对象都是弱引用,即垃圾回收机制不考虑WeakSet对该对象的引用,也就是说, ...
- 使用NIFTI指令画nii图像
❤ 关于几种显示工具 mricro:显示出来的左右脑是反着的: mricroN,SPM,xjview,BrainNetViewer:显示出的左右脑是正确的,并且对于做过仿射变换的图像可以自动识别并且校 ...
- 043医疗项目-模块四:采购单模块—采购单明细查询(Dao,Service,Action三层)
前一篇文章我们做的是在医院的角度上添加在采购单里面添加药品.这一篇文章是查看我们添加的采购单信息. 我们先看一下要实现的效果:当: 按下确认添加时,会在这里 显示出刚才添加的数据. 好,我们就来做这个 ...
- ASP.NET 中执行 URL 重写
具体实现步骤(其中的一种实现方法): 一.下载相关的DLL(ActionlessForm.dll和UrlRewriter.dll) http://download.csdn.net/detail/yi ...