题目链接:

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的更多相关文章

  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. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

  5. Ural 1018 Binary Apple Tree

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在 ...

  6. Ural-1018 Binary Apple Tree(树形dp+分组背包)

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

  7. timus 1018. Binary Apple Tree

    1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...

  8. 【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 ...

  9. poj 2486 Apple Tree(树形DP 状态方程有点难想)

    Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9808   Accepted: 3260 Descri ...

  10. POJ 2486 Apple Tree(树形DP)

    题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...

随机推荐

  1. unity项目git管理

    Unity设置 (关键) Edit -> Project Settings -> Editor -> Version Control Mode 开启 Visible Meta Fil ...

  2. 怎么用PHP发送HTTP请求(POST请求、GET请求)?

    file_get_contents版本: 01 /** 02 * 发送post请求 03 * @param string $url 请求地址 04 * @param array $post_data ...

  3. mysql 中显示 table 的基本信息

    mysql> show table status like 'j_position' \G . row *************************** Name: j_position ...

  4. code.google.com certificate error: certificate is for www.google.com

    有时候我们会碰到下面错误:code.google.com certificate error: certificate is for www.google.com,类似如下: D:\>go ge ...

  5. HDU 1596 最短路变形

    这道题怎么都是TLE,报警了,先放在这 http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <iostream> #includ ...

  6. eclipse 更改背景颜色字体

    原文 切一个自己的图: 废话不说,直接入题. 方式一:替换Eclipse的配置文件 其实Eclipse的各种配置都是在文件设置里的,因此只要用一个配置好的模版来替换默认的配置文件,即可将所有配置克隆到 ...

  7. 牛客Wannafly挑战赛11E 白兔的刁难

    传送门 如果大力推单位根反演就可以获得一个 \(k^2logn\) 的好方法 \[ans_{t}=\frac{1}{k}\sum_{i=0}^{k-1}(w_k^{-t})^i(w_k^i+1)^n\ ...

  8. plan,idea,and dream

    自学机器学习/数据分析/前端 目前想法是从前端入手,学会写/分析网页及其内容/数据,然后使用爬虫爬取数据,然后用机器学习算法对数据进行处理.哈哈,想法是不是太天真了. 学习都从网上的资料入手,因此发现 ...

  9. 移动端H5开发 之 渲染引擎

    渲染引擎 浏览器渲染引擎,负责解析 HTML, CSS,javascript的DOM部分,如桌面浏览器一般手机端也有4个比较重要的渲染引擎 Gecko,Trident,WebKit,Blink . 黑 ...

  10. Manachar算法详解

    求解最长回文串之Manachar算法 问题类型: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段. 回文的含义是:正着看和倒着看相同,如abba和yyxyy. 这类 ...