题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018

Dynamic Programming. 首先要根据input建立树形结构,然后在用DP来寻找最佳结果。dp[i][j]表示node i的子树上最多保存j个分支的最佳结果。代码如下:

 #include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#include <set>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define Infinity 9999999
typedef long long Int;
using namespace std; int dp[][]; //row -> node id, col -> branches preserved. struct link {
int parent;
int child;
int weight;
}; struct tree {
int parent;
tree *left, *right;
int leftw, rightw;
}; int N, Q;
link links[];
bool visited[]; tree * construct(int pid)
{
tree *t = new tree;
t->parent = pid;
int num = ;
bool found = false;
FOR(i, , N - )
{
if (visited[i] == false && (links[i].parent == pid || links[i].child==pid))
{
visited[i] = true;
if (num == )
{
t->leftw = links[i].weight;
if(links[i].parent==pid)
t->left = construct(links[i].child);
else
t->left = construct(links[i].parent);
num++;
}
else if (num == )
{
t->rightw = links[i].weight;
if(links[i].parent==pid)
t->right = construct(links[i].child);
else
t->right = construct(links[i].parent);
found = true;
break;
}
}
}
if (found == false)
{
t->left = NULL;
t->right = NULL;
}
return t;
} int calcuTree(tree *t, int R)
{
if (t == NULL)
return ; int id = t->parent;
if (dp[id][R] == -)
{
int maxLeft = ;
for (int leftR = ; leftR <= R; leftR++)
{
int cleft = ;
int rightR = R - leftR;
if (leftR > )
{
cleft += t->leftw;
cleft += calcuTree(t->left, leftR - );
}
if (rightR > )
{
cleft += t->rightw;
cleft += calcuTree(t->right, rightR - );
} if (cleft > maxLeft)
maxLeft = cleft;
}
dp[id][R] = maxLeft;
}
return dp[id][R];
} int main()
{
while (scanf("%d %d\n", &N, &Q) != EOF)
{
FOR(i, , N - )
{
int p, c, w;
scanf("%d %d %d", &links[i].parent, &links[i].child, &links[i].weight);
} FOR(i, , N - )
visited[i] = false; tree *t = new tree;
t = construct(); FOR(i, , N + )
{
FOR(j, , Q + )
{
dp[i][j] = -;
}
} FOR(i, , N + )
{
dp[i][] = ;
} int ans = calcuTree(t, Q);
printf("%d\n", ans);
}
return ;
}

Ural 1018 Binary Apple Tree的更多相关文章

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

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

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

  3. ural 1018 Binary Apple Tree(树形dp | 经典)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  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. 【URAL 1018】Binary Apple Tree

    http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...

  7. URAL1018 Binary Apple Tree(树形DP)

    题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...

  8. URAL1018 Binary Apple Tree(树dp)

    组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...

  9. URAL1018. Binary Apple Tree

    链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...

随机推荐

  1. js EL 正则表达式

    <script> //校验是否全由数字组成20位数 var patrn=/^[0-9]{1,20}$/; alert(patrn.test("-30000000000" ...

  2. 【mysql】mysql触发器使用示例

    mysql触发器 时间点:before/after 触发事件: update/delete/insert 时间点+触发事件:构成一个完整的触发器的触发时机: 一个触发时机最多只能由1个Trigger: ...

  3. 报错:NoSuchMethodError: kafka.javaapi.PartitionMetadata.leader()Lkafka/cluster/Broker;

    报错现象: 在pom文件添加: <dependency> <groupId>org.apache.kafka</groupId> <artifactId> ...

  4. docker内存监控与压测

    一直运行的docker容器显示内存已经耗尽,并且容器内存耗尽也没出现重启情况,通过后台查看发现进程没有占用多少内存.内存的监控使用的是cadvisor,计算方式也是使用cadvisor的页面计算方式, ...

  5. day2模块初识别

    sys_mod.py import sys print(sys.argv) #['C:/Users/Administrator/desktop/s17/day2/sys_mod.py'] 打印脚本的绝 ...

  6. [UE4]Native Widget Host

    一.Native Widget Host是一个容器,它可以包含一个Slate UI 二.Native Widget Host应该用在当你需要把一个Slate UI 放到UMG中的时候,只有这个时候才需 ...

  7. MySQL视图-(视图创建,修改,删除,查看,更新数据)

    视图是一种虚拟存在的表,对于使用视图的用户来说基本上是透明的.视图并不在数据库中实际存在,行和列数据来自定义视图的查询总使用的表,并且是在使用视图时动态生成的. 视图相对于普通表的优势: 简单:使用视 ...

  8. Android 开发 HandlerThread详解 转载

    转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/75073307 对于Handler不太懂的可以参考我的这两篇文章: Androi ...

  9. CentOS 7 实现ssh无密码登录

    cd ~ 进入根目录. (使用ls -a或者 ls -la 能够看到当前文件夹下的所有文件包含隐藏文件夹等) 我们首先使用ls -la  发现并没有.ssh的文件夹存在. 在终端输入   ssh lo ...

  10. 安装jdk1.9后报 Error:java: 无效的源发行版: 1.9

    现象: intillj IDE 运行main方法 Information:javac 1.8.0_101 was used to compile java sources Error:java: 无效 ...