POJ 2486-Apple Tree(树状背包)
题意
n个节点的树,每个节点都有价值,求在树上最多走k步获得的最大价值(从1开始走,节点的价值只能获取一次)
分析:
开始以为是最基础的树形背包,但后来发现,不能只走子树,有可能还回到根节点,dp[i][j][0]走子树后用回到根,dp[i][j][1],不用回到根
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct tree{
int u,v,next,val;
}t[];
int dp[][][],head[],v[],used[];
int n,len,k;
void add(int a,int b){
t[len].u=a;
t[len].v=b;
t[len].next=head[a];
head[a]=len++;
}
void dfs(int root){
used[root]=;
for(int i=head[root];i!=-;i=t[i].next){
int son=t[i].v;
if(used[son])continue;
dfs(son);
for(int j=k;j>=;--j)
for(int l=;l<=j;++l)
{
dp[root][j][]=max(dp[root][j][],dp[root][j-l][]+dp[son][l-][]);//回根步数减2,不回根步数减1
dp[root][j][]=max(dp[root][j][],dp[root][j-l][]+dp[son][l-][]);
dp[root][j][]=max(dp[root][j][],dp[root][j-l][]+dp[son][l-][]);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&k)){
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
memset(used,,sizeof(used));
for(int i=;i<=n;++i)
{
scanf("%d",&v[i]);
for(int j=;j<=k;++j)
dp[i][j][]=dp[i][j][]=v[i];
}
int a,b;
len=;
for(int i=;i<n-;++i){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs();
printf("%d\n",max(dp[][k][],dp[][k][]));
}
return ;
}
POJ 2486-Apple Tree(树状背包)的更多相关文章
- POJ 2486 Apple Tree [树状DP]
题目:一棵树,每个结点上都有一些苹果,且相邻两个结点间的距离为1.一个人从根节点(编号为1)开始走,一共可以走k步,问最多可以吃多少苹果. 思路:这里给出数组的定义: dp[0][x][j] 为从结点 ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- POJ 2486 Apple Tree ( 树型DP )
#include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...
- poj 2486 Apple Tree (树形背包dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: poj-2486 题意 给一个n个节点的树,节点编号为1~n, 根节点为1, 每个节点有一个权值. 从 ...
- POJ 3345-Bribing FIPA(树状背包)
题意: 有n个国家投票,要得到一个国家的投票有一定的花费,如果给到一个国家的票同时也得到了它所有附属国的票,给出国家关系树,求至少得到m票的最小花费. 分析:基础树状背包,dp[i][j],以i为根的 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
随机推荐
- OO之观察者模式
以下为观察者模式详解: 引子: 假设有这样一个问题,有一条河经过一个山谷,山谷下有一个村庄,人们在山谷处修建了一个水库,并安排专人管理,当水库的水位过高时要通知下游居民注意水库的开闸放水,当水库的水温 ...
- [搜片神器]BT种子下载超时很多的问题分析
继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: h31bt.org 大家可以给提点意 ...
- springMVC从上传的Excel文件中读取数据
示例:导入客户文件(Excle文件) 一.编辑customer.xlsx 二.在spring的xml文件设置上传文件大小 <!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1 ...
- @Repository、@Service、@Controller 和 @Component(转)
鸣谢:http://blog.csdn.net/ye1992/article/details/19971467 @Repository.@Service.@Controller 和 @Componen ...
- What the hell is Rotate?
- Firefly安装说明 与 常见问题
原地址:http://bbs.gameres.com/thread_223688.html 第三方库依赖: twisted, python-memcached ftp://ftp.tummy.c ...
- Python 异常结构
http://flyheaven.blog.163.com/blog/static/7401172201193085243920/ 1.Python内建异常体系结构 The class hierarc ...
- 发现一个可以在线运行JS代码的网站
平时可以在这里玩 http://jsbin.com/
- 关于static继承的问题
c++primer 15.2.7节关于static继承的意思是,父类和子类共享static函数或者static成员变量,并且子类要访问还要受它们的权限限制,下面是看到的另一个例子 class Base ...
- nginx比较apache
http://blog.csdn.net/hanghangaidoudou/article/details/8506963 话说nginx在大压力的环境中比apache的表现要好,于是下载了一个来折腾 ...