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 ...
随机推荐
- asp.net服务器控件开发系列一
最近想写写博客记录下自己学习开发服务器控件. 第一步:搭建环境. 1.新建一个项目类库,用于保存控件: 2.新建一个Web工程,用于调用控件: 如图: 第二步:在控件类库下,新建一个服务器控件类Tex ...
- [Twisted] 部署Twisted
Twisted提供了基础设施,来实现可重用.可配置的方式来部署. 1.Service Twisted使用Service来实现了许多协议,如TCP,FTP,HTTP,SSH等. 实现的IService接 ...
- 2014年10月30日-----SQL的基础知识
数据库的概念 结构化查询语言:structured query language 简称:SQL 数据库管理系统:database management system 简称:DBMS 数据库管理员:da ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- 【感悟】看Hyouka的感想 (1)
最近偶然从B站看到了<冰菓>这个(个人觉得是推理)番 我突然觉得自己曾经做的一些行为欠妥 有才能者的不自知,是对无才能者的讽刺 举个例子就是:即当别人说你很牛的时候,你却说你只 ...
- Visual Studio 2008中添加运行按钮 转载
在Visual Studio 2008中添加运行按钮 默认情况下,VS2008中的工具栏上没有运行按钮,只有调试(Debug)按钮,可按照以下方法添加 1.点击菜单Tools(工具)->Cust ...
- 交叉编译tslib1.4
cross-compiler: arm-linux-gcc V4.2.1 source code: tslib-1.4.tar.gz #tar zxvf tslib-1.4.tar.gz #./aut ...
- 安装ImageMagick扩展出现configure: error: not found. Please provide a path to MagickWand-config or Wand- config program
安装ImageMagick扩展报错: checking ImageMagick MagickWand API configuration program... checking Testing /u ...
- Win7 下,离线安装 Android Studio 1.0.1 的方法
此教程没有亲自动手试过,先保存在这里 http://download.csdn.net/detail/tuobaxiao2008/8268281
- jQuery.Deferred对象
一.前言 jQuery1.5之前,如果需要多次Ajax操作,我们一般会使用下面的两种方式: 1).串行调用Ajax $.ajax({ success: function() { $.ajax({ su ...