POJ 2486 Apple Tree(树形dp)
http://poj.org/problem?id=2486
题意:
有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值。(每个点只能获得一次)
思路:
从1点开始,往下dfs,对于每个结点,把时间分配给它的子节点,然后求一个最大值。
但是要注意的是,它有可能会走了之后然后又走回到父亲结点,这样的话,状态转移方程需要多考虑一下。
d【u】【j】【0/1】表示从u结点出发走 j 时所能获得的最大权值(0/1表示是否返回u结点)。
d[u][j][]=max(d[u][j][],d[u][j-t][] + d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][] + d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][] + d[v][t-][]);

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n, k; int val[maxn];
int vis[maxn]; vector<int> g[maxn]; int d[maxn][maxn][]; void dfs(int u)
{
vis[u]=;
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(vis[v]) continue;
dfs(v);
for(int j=k;j>=;j--)
{
for(int t=;t<=j;t++)
{
d[u][j][]=max(d[u][j][],d[u][j-t][]+d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][]+d[v][t-][]);
d[u][j][]=max(d[u][j][],d[u][j-t][]+d[v][t-][]);
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n, &k))
{
for(int i=;i<=;i++) g[i].clear();
memset(d,,sizeof(d)); for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
for(int j=;j<=k;j++)
d[i][j][]=d[i][j][]=val[i];
} for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
} memset(vis,,sizeof(vis));
dfs();
printf("%d\n",max(d[][k][],d[][k][]));
}
return ;
}
POJ 2486 Apple Tree(树形dp)的更多相关文章
- 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 ...
- POJ 2486 Apple Tree (树形dp 经典题)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const ...
- 【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,树形背包)
题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数. 思路: 既然是树,而且有限制k步,那么树形DP正好. 考虑1个点的情况:(1)可能在 ...
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
- poj 2486 Apple Tree (树形背包dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: poj-2486 题意 给一个n个节点的树,节点编号为1~n, 根节点为1, 每个节点有一个权值. 从 ...
- POJ 2486 Apple Tree ( 树型DP )
#include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...
随机推荐
- js offset
1.offsetParent offsetParent属性返回一个对象的引用,这个对象是距离调用offsetParent的元素最近的(在包含层次中最靠近的),并且是已进行过CSS定位的容器元素. 如果 ...
- gradle多项目 svn依赖
当svn有多个子项目且没有根项目的时候,用eclipse拷贝下来是容易出问题的,经常子项目之间的依赖会有问题,还是推荐用IDEA. 操作说明: 如果SVN有 A,B,C,D四个项目,A为web项目,B ...
- 神奇的随机数rand()
随机数在统计理论中占有很重要的地位,很多应用都需要用到他,系统自带的rand()函数产生的是伪随机数,所以该如何真正的随机数呢? 1.c语言中的时间函数 关于c语言我查到的两个比较好的博文是:http ...
- 使用异步消息处理更新UI线程
1. Android的UI时线程不安全的,如果在子线程中更新UI会出现异常,导致程序崩溃. 为了解决如上这些问题,我们常用的做法就是使用Android的异步消息机制实现即可(创建一个Message对象 ...
- CodeForces 25C(Floyed 最短路)
F - Roads in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 沈阳网络赛F-Fantastic Graph【贪心】or【网络流】
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
- IoC COntainer Create Javabeans 可以通过读取beans.xml 文件来创建一个应用程序上下文对象 依赖反转
Spring初学快速入门 - Spring教程™ https://www.yiibai.com/spring/spring-tutorial-for-beginners.html# pom <? ...
- 设置webView头部不能滑动
设置webView头部不能滑动 _webView.scrollView.bounces=NO;
- jquery tab选项卡、轮播图、无缝滚动
最近做一个页写了一个星期,觉得自己对jquery还是很不熟悉 自己查了一下资料写了几个封装好的tab选项卡.轮播图.无缝滚动 $(function(){ //tab选项卡 jQuery.tab=fun ...
- 浅谈vuex
很多技术,刚接触的时候:这是啥?用的时候:哟嚯,是挺好用的!加以研究:卧槽,就是这么个逼玩意儿! 最近接手了一个别人写了1/5的vue项目(页面画了1/3,接口啥都没对); 对于表格中的数据项操作以及 ...