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. BZOJ1015[JSOI2008]星球大战starwar[并查集]

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit ...

  2. UNITY在VS中调试

    下载地址:https://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=RootCategory&f%5B0 ...

  3. Makefile规则③规则语法、依赖、通配符、目录搜寻、目标

    规则语法 通常规则的语法格式如下: TARGETS : PREREQUISITES COMMAND ... 或者: TARGETS : PREREQUISITES ; COMMAND COMMAND ...

  4. ajax asud模板

    <table class="table"> <tr> <th>@Html.DisplayNameFor(model=>model.Id)& ...

  5. Oracle 多表update

    今天凌晨因为要在数据库里做一些操作,是关于两表关联的update,但语句怎么写都不正确,老是报错,于是心惊肉跳(就怕不能及时完成操作)去查了一下,NND,原来把SQL写成了在SQL Server下面的 ...

  6. DPABI advanced edition 文件夹组织形式

    ❤ Regress out nuisance: ... csf\wm ('SPM apriori') {working directory}\Masks\WarpedMasks\SubIndex_Wh ...

  7. ext 自带搜索功能

  8. swift UIImage加载远程图片和圆角矩形

    UIImage这个对象是swift中的图像类,可以使用UIImageView加载显示到View上. 以下是UIImage的构造函数: init(named name: String!) -> U ...

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

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

  10. Linux 信号详解三(sleep,raise)

    sleep()函数 .sleep()函数作用:让进程睡眠 .能被信号打断,然后处理信号函数以后,就不再睡眠,直接向下执行代码 .sleep函数的返回值是剩余秒数 //sleep 函数 #include ...