Ural-1018 Binary Apple Tree(树形dp+分组背包)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = ;
int dp[maxn][maxn]; //dp[i][j]表示以i为根,保留j个点的最大权值。
int N,Q;
int G[maxn][maxn];
int num[maxn]; //以i为根的树的节点个数。 //这里处理的时候要注意可以把边的权值压倒儿子节点。
void dfs(int u,int fa){
num[u] = ;
for(int v=;v<=N;v++){
if(!G[u][v] || v == fa) continue; dfs(v,u);
num[u] += num[v];
} for(int v=;v<=N;v++){ //相当于分组k。
if(!G[u][v] || v == fa) continue; for(int i=num[u];i>;i--){ //相对于下面的V -> 0
for(int j=;j<i&&j<=num[v];j++) //枚举组k中的元素。
dp[u][i] = max(dp[u][i],dp[u][i-j]+dp[v][j]+G[u][v]);
}
}
}
/**
f[k][v]表示前k组物品花费费用v能取得的最大权值
f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品i属于组k}
分组背包一维数组的伪代码:
for 所有的组k
for v=V..0
for 所有的i属于组k
f[v]=max{f[v],f[v-c[i]]+w[i]}
**/
int main()
{
cin>>N>>Q;
memset(G,,sizeof(G));
memset(dp,,sizeof(dp));
for(int i=;i<N;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
G[u][v] = G[v][u] = w;
}
dfs(,-);
printf("%d\n",dp[][Q+]);
}
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建立树形结构,然后在 ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- poj2486 Apple Tree (树形dp+分组背包)
题目链接:https://vjudge.net/problem/POJ-2486 题意:一棵点权树,起点在1,求最多经过m条边的最大点权和. 思路: 树形dp经典题.用3维状态,dp[u][j][0/ ...
- 【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 ...
随机推荐
- zoj1276矩阵连乘dp
很经典的入门dp /*******************************************************************************/ /* OS : 3 ...
- YZOI Easy Round 2_化简(simplify.c/cpp/pas)
Description 给定一个多项式,输出其化简后的结果. Input 一个字符串,只含有关于字母x 的多项式,不含括号与分式,没有多余的空格. Output 一个字符串,化简后的多项式,按照次数从 ...
- 关于Memcache使用的工具类
分布式缓存有它自己的好处 . 下面的 ConstValue.sessionId 是一个常量 public static readonly string sessionId = "sessi ...
- JDK源码阅读(三) Collection<T>接口,Iterable<T>接口
package java.util; public interface Collection<E> extends Iterable<E> { //返回该集合中元素的数量 in ...
- go bytes缓冲区使用介绍
缓冲区原理简介: go字节缓冲区底层以字节切片做存储,切片存在长度len与容量cap, 缓冲区写从长度len的位置开始写,当len>cap时,会自动扩容.缓冲区读会从内置标记off位置开始读(o ...
- 代理服务器基本知识普及代理IP使用方法!
本文并未从专业角度进行详细讲解,而是从应用的角度出发来普及一些代理服务器的基本知识.文章明显是搜集多方资料的拼凑,而且比较老了,但往往越老的东西越接近事物的本质,更容易窥探到原理,对于刚接触的人来说, ...
- c# 把 颜色值Hex 转换为 Color
原文: http://abujj.me/archives/695 Assuming you mean the HTML type RGB codes (called Hex codes, such a ...
- bzoj 3041: 水叮当的舞步 迭代加深搜索 && NOIP RP++
3041: 水叮当的舞步 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 72 Solved: 44[Submit][Status] Descript ...
- iOS便捷开发工具分享
项目/代码优化工具 1.objec_dep,可以了解项目中各个类的关联信息,了解项目中无效文件,知道双向应用的文件. 下载地址: https://github.com/nst/objc_dep 2.b ...
- UVA 1329 - Corporative Network
带权值的并查集的应用: 代码: #include<cstdio> #include<algorithm> #include<cmath> #include<c ...