Ural 1018 Binary Apple Tree
题目链接: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的更多相关文章
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- 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 ...
- ural 1018 Binary Apple Tree(树形dp | 经典)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- 【URAL 1018】Binary Apple Tree
http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...
- URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...
- URAL1018 Binary Apple Tree(树dp)
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...
- URAL1018. Binary Apple Tree
链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...
随机推荐
- [SQL]某数据库中查出包含 字段名 的所有表名
--利用SQL语句来查询字段所在的表 --从某数据库中查出包含 字段名 字段的所有表名 SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE ...
- 第一课了解SQL
1.1 数据库基础 其实一直在使用这数据库,当你在邮箱中查询名字时,当在网站上进行搜索,在自动取款机上使用ATM卡,这些操作都是在使用数据库 1.1.1 数据库 数据库:保存有组织的数据的容器(通 ...
- bitbucket 上公钥SSH key如何add key并进行项目运用
前提:从sourcetree 添加项目时老是拉取不下来,查到原因是应为bitbucket需要SSH key公钥 目的:公钥相当于你在任何一台电脑只要有公钥授权就可以随时提交代码到服务器 原因: 1.很 ...
- BZOJ 2173 luoguo P4451 [国家集训队]整数的lqp拆分
整数的lqp拆分 [问题描述] lqp在为出题而烦恼,他完全没有头绪,好烦啊… 他首先想到了整数拆分.整数拆分是个很有趣的问题.给你一个正整数N,对于N的一个整数拆分就是满足任意m>0,a1 , ...
- spring cloud 学习目录
1.spring cloud简单示例 2.spring cloud快速入门 3.spring cloud 常用 4.spring cloud 原理 5.spring cloud 源码分析 6.spri ...
- 中间件weblogic控制台创建数据源报错---根据真实故事改编
1.在weblogic控制台创建数据源,有报错--不能创建数据源,图免 2.weblogic数据源管理节点所在服务器telnet测试,到数据库1521端口是通的 3.经过请教各路大神,得出结论:由于子 ...
- easy ui 关闭选项卡
var tab = window.parent.getCurrentTab(); var tabs = window.parent.getTabs(); var index = tabs.tabs(& ...
- a.vim 插件
引用自 https://blog.csdn.net/zhangsming/article/details/42652695 vim插件之快速切换头(.h)/源(.c,.cpp,.cc)文件——a.v ...
- python,关于用户登录与注册问题
tag=Truecount=1while tag: name = input('请输入用户名>>:').strip() password = input('请输入密码>>:') ...
- 动画优化、客户端存储、历史记录、worker
一.requestAnimationFrame 1.requestAnimationFrame怎么用? 设置关键帧动画效果,注重关键帧执行的情况,用法与setTimeout一样 2.requestAn ...