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的更多相关文章

  1. 【LeetCode 173】Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  2. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  3. 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 ...

  4. timus 1018. Binary Apple Tree

    1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...

  5. BNUOJ 13358 Binary Apple Tree

    Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...

  6. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. 【leetcode】Binary Search Tree Iterator

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  8. 【BZOJ 1018】 [SHOI2008]堵塞的交通traffic

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 [题意] [题解] 按照这里的题解写的http://blog.csdn.net/ ...

  9. 【BZOJ 4353】 Play with tree

    [题目链接] 点击打开链接 [算法] 树链剖分 对于线段树的每个节点,记录这段区间的最小值,最小值的个数,值为0的个数,此外,还要维护两个懒惰标记 [代码] 本题细节很多,写程序时要认真严谨! #in ...

随机推荐

  1. 【2016-10-13】【坚持学习】【Day4】【virtual 虚函数】

    定义一个基类,有一个虚函数 定义三个子类,分别继承,重写,New,这个虚函数   abstract class Test { public virtual void Prinf() { Console ...

  2. HDU 5155 Harry And Magic Box --DP

    题意:nxm的棋盘,要求每行每列至少放一个棋子的方法数. 解法:首先可以明确是DP,这种行和列的DP很多时候都要一行一行的推过去,即至少枚举此行和前一行. dp[i][j]表示前 i 行有 j 列都有 ...

  3. CF 371C-Hamburgers[二分答案]

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. extra增强延迟加载

    这种配置和配置为lazy=true是一样的,但它的好处在于调用size/contains等方法时,并不查询整个集合的数据,而是发送一条sql语句来处理,只有真正在使用时才全部去查询整个集合

  5. hdu 2586 How far away

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. 2016 08 27 印刷菜单增加sql语句

    insert into `module` (`ID`, `CONSONANTCODE`, `CREATEDATE`, `DESCRIPTION`, `HANDLER`, `HASCHILD`, `IC ...

  7. 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题

    下面两种现象,用同一种方法解决 1.解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题 2.突然有一天首页访问图片很慢,至少隔20多秒所有图片才会出来.(解析:app使 ...

  8. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  9. 学习C++.Primer.Plus 4 复合类型

    本章介绍的有复合类型有: 数组. 字符串. 结构. 共用体. 指针 数组: 声明数组时数组长度必须为常量(或const). 只有初始化时可以用“=”,其它时候均不可以给数组直接赋值,除了赋值的元素以外 ...

  10. 前端见微知著工具篇:Grunt实现自动化

    转载说明 本篇文章为转载文章,来源为[前端福利]用grunt搭建自动化的web前端开发环境-完整教程,之所以转载,是因为本文写的太详细了,我很想自己来写,但是发现跳不出这篇文章的圈子,因为写的详尽,所 ...