【URAL 1018】Binary Apple Tree
2016-09-25 10:12
309 查看
http://vjudge.net/problem/17662
loli蜜汁(面向高一)树形dp水题
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)
- URAL 1018 Binary Apple Tree
- [ural 1018]Binary Apple Tree[树DP]
- URAL 1018 Binary Apple Tree 简单树形背包
- ural 1018-Binary Apple Tree【树状DP】
- Ural 1018 Binary Apple Tree 树形dp
- ural 1018 binary apple tree
- URAL 1018 Binary Apple Tree(树形dp入门题)
- Ural-1018-Binary Apple Tree
- URAL 1018 Binary Apple Tree(树DP)
- ural 1018 Binary Apple Tree
- URAL 1018 Binary Apple Tree (树形DP)
- ural 1018 Binary Apple Tree(树dp)
- URAL_1018 Binary Apple Tree 树形DP+背包
- URAL 1018 binary apple tree
- Ural 1018 binary apple tree(显性树的树dp)
- URAL 1018 Binary Apple Tree
- ural 1018 Binary Apple Tree(树形dp | 经典)
- ural 1018 Binary Apple Tree(树形DP)
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)