URAL-1018 Binary Apple Tree---树形DP
题目链接:
https://cn.vjudge.net/problem/URAL-1018
题目大意:
给你一棵树,每条边有一个边权,求以1为根节点,q条边的子数(q+1个点),边权和至最大。
解题思路:
dp[root][j], 表示以root为根节点,保留j个节点的最大边权和。
dp[root][j]=max(dp[root][j],dp[root][j-t]+dp[son][t]+len);
t的范围从1到j - 1,因为每个点从dp[][1]开始更新
#include<bits/stdc++.h>
using namespace std;
const int maxn = + ;
typedef long long ll;
struct node
{
int v, w;
node(){}
node(int v, int w):v(v), w(w){}
};
vector<node>Map[maxn];
int num[maxn];//num[i]表示以i节点为root的子树中的点的数目
int dp[maxn][maxn];//dp[i][j]表示以i节点为root的子树中只有j条边最大权值
void dfs(int root, int fa)
{
num[root] = ;
for(int i = ; i < Map[root].size(); i++)
{
int v = Map[root][i].v, w = Map[root][i].w;
if(v == fa)continue;//不可回溯
dfs(v, root);//先将儿子信息更新好
num[root] += num[v];//root子树中当前的节点数目
for(int j = num[root]; j >= ; j--)//更新父节点的dp
{
for(int k = ; k < j && k <= num[v]; k++)//k不能等于j,k=j时说明root的点数目为0
dp[root][j] = max(dp[root][j], dp[root][j - k] + dp[v][k] + w);
}
}
}
int main()
{
int n, k;
while(scanf("%d%d", &n, &k) != EOF)
{
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Map[u].push_back(node(v, w));
Map[v].push_back(node(u, w));
}
dfs(, -);
cout<<dp[][k + ]<<endl;//包含k条边,也就是k+1个点
}
return ;
}
URAL-1018 Binary Apple Tree---树形DP的更多相关文章
- 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 ------------------------------------------------------------ ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
- Ural 1018 Binary Apple Tree
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在 ...
- Ural-1018 Binary Apple Tree(树形dp+分组背包)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- 【POJ 2486】 Apple Tree (树形DP)
Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...
- poj 2486 Apple Tree(树形DP 状态方程有点难想)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9808 Accepted: 3260 Descri ...
- POJ 2486 Apple Tree(树形DP)
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...
随机推荐
- 【angular5项目积累总结】优秀组件以及应用实例
1.手机端 图片预览组件 组件:sideshow 效果图:(预览图全屏 且可以左右移动) code: <div class="row ui-app-s ...
- 微信WeUI扩展组件
主要包括 下拉刷新pullToRefresh downRefresh.html 主要的代码是$(document.body).pullToRefresh(); <div class=" ...
- Python——爬虫学习2
BeautifulSoup插件的使用 这个插件需要先使用pip安装(在上一篇中不再赘言),然后再程序中申明引用 from bs4 import BeautifulSoup html=self.requ ...
- Knockout.js Attr绑定
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- 一、hadoop单节点安装测试
一.hadoop简介 相信你或多或少都听过hadoop这个名字,hadoop是一个开源的.分布式软件平台.它主要解决了分布式存储(hdfs)和分布式计算(mapReduce)两个大数据的痛点问题,在h ...
- HDU 2544(简单最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2544 /* 使用pair代替结构 */ #include <iostream> #include & ...
- Struts2-------领域对象
领域对象 这个词汇是我在刚开始学习Struts2的时候接触到的,然后再网上查找了一些相关文档,说的最多的就是领域模型.说的挺文字的,自己还是不明白.Model也可说是“领域对象”,包含属性和行为. 好 ...
- 您只能在 HTML 输出流中使用 document.write,啥意思
JavaScript :写入到HTML输出语法 注意:只能在HTML输出中使用,如果在文档已经加载后使用(比如在函数中) 会覆盖到整个文档 <!DOCTYPE html> <htm ...
- bitset(01串)优化
bitset的经典使用: 见代码及注释: #include<bitset> #include<algorithm> using namespace std; //只需调用< ...
- drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法二
drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法一 中介绍了ckeditor的安装和配置方法,其实还有另一种新方法,不用IMCE模块. 不过需要ckfinder的JS库,可以 ...