题目链接: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. Bootstrap 插件收集

    Bootstrap-Mutilselect  将下拉选项扩展支持多选以及多种选择方式 http://davidstutz.de/bootstrap-multiselect/ Bootstrap Sel ...

  2. jmeter分布式、linux运行

    一.jmeter分布式压测(多台电脑一起压测) 1.有多台电脑,每台电脑上都有jmeter,而且这几台电脑都互相能ping通 2.在我的电脑的jmeter,bin目录下,修改jmeter.proper ...

  3. python之路——10

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 a.函数可读性强,复用性强 def 函数名() 函数体 return 返回值 函数先定义后执行, b. ...

  4. [UE4]Text Box

    Text Box:文本输入控件. 一.新建一个名为testTextBox的UserWidget,添加一个名为“EditableTextBox_0”的TextBox到默认容器Canvas Panel 二 ...

  5. Linux集群之keepalive+Nginx

    集群从功能实现上分高可用和负载均衡: 高可用集群,即“HA"集群,也常称作“双机热备”. 当提供服务的机器宕机,备胎将接替继续提供服务: 实现高可用的开源软件有:heartbeat.keep ...

  6. Twisted网络库编程实例

    于这一周看了python的第三方网络库Twisted,英文看的头比较大,想看英文的话点击这里.如果英文很烂,可以看中文,这里.总的来说我了解到的主要包括以下三个东东:Factory.protocol和 ...

  7. Spring线程池的5个要素

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...

  8. PyQt--QTreeWidget

    转载:loster_Li QTreeWidget的继承关系如下图: 因为继承关系是 QAbstractItemView->QTreeView->QTreeWidget  ,所以和QTabl ...

  9. 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器

    update明显更新就一行,但是结果显示更新多行. 原因是有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器

  10. setDaemon 守护线程

    setDaemon(True): 将线程声明为守护线程,必须在start() 方法调用之前设置, 如果不设置为守护线程程序会被无限挂起.这个方法基本和join是相反的. 当我们 在程序运行中,执行一个 ...