【URAL 1018】Binary Apple Tree

时间:2022-02-13 14:22:00

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 二叉苹果树 &sol; 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&period; 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】 &lbrack;SHOI2008&rsqb;堵塞的交通traffic

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

  9. 【BZOJ 4353】 Play with tree

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

随机推荐

  1. oracle计算是否是同一周

    函数已经解决跨年问题 select to_char(date'2016-12-31','iW') from dual; select to_char(date'2017-01-01','iW') fr ...

  2. SQL Server连接Oracle详细步骤

    http://blog.csdn.net/weiwenhp/article/details/8093105 我们知道SQL Server和Oracle其实很多原理都类似.特别是一些常用的SQL语句都是 ...

  3. 编写一个程序实现strcat函数的功能

    写自己的strcat函数------→mycat #include <stdio.h> #include <string.h> #define N 5 char *mycat( ...

  4. 解决虚拟内存不够导致Eclipse is not responding

    安装目录下eclipse.ini中: 修改参数至必要大小. e.g. -vmargs-Djava.net.preferIPv4Stack=true-Dosgi.requiredJavaVersion= ...

  5. org&period;eclipse&period;birt&period;report&period;data&period;oda&period;jdbc&period;JDBCException&colon; Missing properties in Connection&period;open&lpar;Propertie

    首先查看project的web.xml档"BIRT_RESOURCE_PATH"属性的设置.此属性设置的是"用户资源存放路径.这些资源包含 library 文件,imag ...

  6. vue 路由嵌套高亮问题

    正常路由嵌套是没有问题的,但是如果你已经在当前主路由页面了,然后再次点击主路由就会出现页面数据空白的情况 看代码: //主路由通过v-for循环出来 <div class="list- ...

  7. Mybatis 常用注解

    Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...

  8. python数据抓取分析(python &plus; mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

  9. HDU - 6444 Neko&&num;39&semi;s loop&lpar;循环节&plus;最大子段和&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=6444 题意 一个有n个数的环,每次循环走k步,走到每个点都有具体的权值,问在任意点出发最多走m步的情况下,一开始 ...

  10. Qt&colon; 记事本源代码

    界面编程之实例学习,系统记事本是个极好的参考,初学Delphi及后之c#,皆以记事本为参考,今以Qt学习,亦是如此. 期间搭建开发环境,复习c++知识,寻找模块对应功能,不一而足:现刻录其模块代码,以 ...