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 ...
随机推荐
- WINDOWS NT操作系统的注册表文件
WINDOWS NT操作系统的注册表文件 WINDOWS NT注册表文件分为系统文件和用户文件两类. 系统设置和缺少用户 配置数据存放在系统C:\Windows\System32\config文件夹下 ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- [转]Java事件处理机制- 事件监听器的四种实现方式
原文来自http://stefan321.iteye.com/blog/345221 自身类作为事件监听器 外部类作为事件监听器 匿名内部类作为事件监听器 内部类作为事件监听器 自身类作为事件监听器: ...
- 转wave 文件解析
转 1 WAVE 文件格式分析 WAVE 文件作为多媒体中使用的声音波形文件格式之一,它是以RIFF(Resource Interchange File Format)格式为标准的.每个WAVE文件的 ...
- 杂谈c.cpp
Endl确保程序运行前刷新输出(将其立即显示在屏幕上),而’\n’却不能保证 在c++中,是以分号分割的,所以可以把几条代码放在一行,也可以把一条代码放在几行 编译器负责分配和标记内存的细节 C++可 ...
- Python笔记:深浅拷贝
1.赋值操作两者是同一数据,其内存地址一样.适用于list.dict.set数据类型. 2.copy是浅拷贝,只能拷贝嵌套数据的第一层数据,嵌套的数据与赋值操作相同,其内存地址一样,当一个被更改,其他 ...
- C# Excel添加超链接
操作当前单元格(关键代码就两行) Range range = (Range)ExSheet.Cells[i + 2, j + 1]; ...
- iphone 开发h5 踩过的坑
html,body{ -webkit-text-size-adjust: none; } // 当需要在中文版chrome浏览器中显示小于12px的字体时,而且此时页面放大效果会被阻止 html,b ...
- Sql入门学习——基本语法函数
--------使用工具navicat 快捷键操作 --------基本操作DML &DDL --------运算比较 --------数据类型 --------常用函数 --------常用 ...
- js:作用域总结1
先说几个概念: 1.js代码从上往下执行 2.变量提升: 变量提升是浏览器的一个功能,在运行js代码之前,浏览器会给js一个全局作用域叫window,window分两个模块,一个叫内存模块,一个叫运行 ...