Apple Tree

Description

Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.

Input

There are several test cases in the input 
Each test case contains three parts. 
The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200) 
The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i. 
The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent. 
Input will be ended by the end of file.

Note: Wshxzt starts at Node 1.

Output

For each test case, output the maximal numbers of apples Wshxzt can eat at a line.

Sample Input

2 1
0 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output

11
2
【题意】
  一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值。 【分析】
  这题做太久了我太傻逼了所以要写博客。
  

  每天都智障24小时。。一开始还看错范围,醉。。
  这题感觉很依赖,于是我首先打dfs序那种的树形依赖,然后发现我不会合并。
  于是我去打子树版本的,搞了超级久,整个一傻逼。
  首先问题是我们是可以往回走,所以设f[i][j][0]表示在点i的子树上走j步不往回走到i的最值,f[i][j][1]表示往回走。
  一个点的子树中不往回走只能有一个,注意不要漏啊不要重啊就好了。
  f[x][j][1]=max(f[x][j][1],f[y][l][1]+f[x][j-l-1][1])
  f[x][j][0]=max(f[x][j][0],f[y][l][0]+f[x][j-l][1])
  f[x][j][0]=max(f[x][j][0],f[y][l][1]+f[x][j-l-1][0])

  有一个地方就是一个子树假设大小是size[x],那么在这棵子树上走的步数不会超过2*size[x],因为每条边最多走两次(走下去走上去),所以可以for到子树大小的2倍,这样可以优化到n^2。

  感觉跟前面的依赖型还是有点不一样,dfs序版的就不能解决,貌似,因为啊回来不回来那个好难搞

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0xfffffff
#define Maxn 210 struct node
{
int x,y,next;
}t[*Maxn];int len; int n,k;
int first[Maxn],a[Maxn]; int mymax(int x,int y) {return x>y?x:y;} void ins(int x,int y)
{
t[++len].x=x;t[len].y=y;
t[len].next=first[x];first[x]=len;
} int f[Maxn][Maxn][],ans; void ffind(int x,int fa)
{
f[x][][]=f[x][][]=a[x];
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
ffind(y,x);
for(int j=k;j>=;j--)
{
for(int l=;l<=j;l++)
{
f[x][j][]=mymax(f[x][j][],f[y][l][]+f[x][j-l-][]),//l+1
f[x][j][]=mymax(f[x][j][],f[y][l][]+f[x][j-l][]),
f[x][j][]=mymax(f[x][j][],f[y][l][]+f[x][j-l-][]);
// f[x][l+1][0]=mymax(f[x][l+1][0],f[y][l][0]);
}
}
}
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
ans=-INF;
k++;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
len=;
memset(first,,sizeof(first));
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
memset(f,-,sizeof(f));
ffind(,);
for(int i=;i<=k;i++) ans=mymax(ans,mymax(f[][i][],f[][i][]));
printf("%d\n",ans);
}
return ;
}

[POJ 2486]

2016-10-15 11:19:19

 
												

【POJ 2486】 Apple Tree (树形DP)的更多相关文章

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

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

  2. POJ 2486 Apple Tree(树形DP)

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

  3. POJ 2486 Apple Tree (树形dp 经典题)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const ...

  4. POJ 2486 Apple Tree (树形DP,树形背包)

    题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数. 思路: 既然是树,而且有限制k步,那么树形DP正好. 考虑1个点的情况:(1)可能在 ...

  5. POJ 2486 Apple Tree

    好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...

  6. URAL_1018 Binary Apple Tree 树形DP+背包

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

  7. POJ 2486 Apple Tree(树形dp)

    http://poj.org/problem?id=2486 题意: 有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值.(每个点只能获得一次) 思路: 从1点开始,往下dfs,对于每个 ...

  8. poj 2486 Apple Tree (树形背包dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: poj-2486 题意 给一个n个节点的树,节点编号为1~n, 根节点为1, 每个节点有一个权值.    从 ...

  9. POJ 2486 Apple Tree ( 树型DP )

    #include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...

随机推荐

  1. find命令基本使用一览

    find命令相对于locate这种非实时查找的搜索命令,大大增加了我们搜索的便捷度以及准确性:并且能够方便的帮助我们对大文件.特定类型的文件查找与删除,特别是有超多小碎文件的时候,更是方便至极.... ...

  2. 使用POI操作Excel使用小总结

    1. Workbook维护一个调色板,可以自定义设置56种颜色,下标从8到63. 用到颜色的地方,可以输入下标获取颜色,如CellStyle的setFillForegroundColor(); 2.C ...

  3. [转]SharePoint 2010/2013 使用Javascript来判断权限的三种方法

    本文讲述SharePoint 2010/2013 使用Javascript来判断权限的三种方法的实现方式及其优缺点. 1. 根据用户所在的SharePoint组(比如用户在Leader 组才可以使用审 ...

  4. php文件上传之单文件上传

    为了简单一些,php文件跟form表单写在了一个文件里. php单文件上传----> <!DOCTYPE html> <html> <head> <me ...

  5. Unity3D 之UGUI 图片

    这里来降价下Unity3Dl的图片 先创建一个图片 图片的属性 Preserve Aspect -->保持图片的原始宽高比例 Set native Size -->图片原始尺寸 Image ...

  6. Orchard 学习-手动安装Orchard

    通过Orchard zip 文件手动配置网站 这篇文章将引导你如果通过Zip文件来安装Orchard. 我们会使用三种不同的方法来承载Orchard: IIS. WebMatrix and IIS E ...

  7. Sql触发器脚本

    ALTER Trigger [dbo].[test] --新建触发器 On [dbo].[test1] --在test1表中创建触发器 for insert --触发条件 As --事件触发后所要做的 ...

  8. 20160427Struts2--入门1

    参考资料来自传智播客:非原创,只是做个笔记: 一.Struts2简介: Struts2是在WebWork2基础发展而来的.和struts1一样, Struts2也属于MVC框架.不过有一点大家需要注意 ...

  9. nyoj914Yougth的最大化(二分搜索 + 贪心)

    Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗 ...

  10. Apache+Tomcat +mod_proxy集群负载均衡及session

      序言: 在玩Apache+Tomcat +mod_jk集群负载均衡及session的时候发现,还有一种方式可以实现,就是网上各位大牛们说的mod_proxy反向代理. 实在弄的我的知识细胞洋洋.实 ...