POJ 2486 Apple Tree ——(树型DP)

  题意是给出一棵树,每个点都有一个权值,从1开始,最多走k步,问能够经过的所有的点的权值和最大是多少(每个点的权值只能被累加一次)。

  考虑到一个点可以经过多次,设dp状态为dp[i][j][k],i表示当前从i出发,j表示最多走j步,k=0的话表示最后回到i点,否则不回到i点的子问题的答案。

  转移见代码。

  值得注意的是dfs中j循环的方向必须从大到小,因为如果从小到大,当前的j是从比其小的dp[u][j-t][...]更新过来的,而后者是根据走了v以后的更新过来的。换言之,如果从小到大,那么,走v贡献的权值就会被计算多次了,而题目中给的条件是,一个点的权值只能计算一次。拓展一下的话,如果一个点的权值可以被计算多次,那么应当从小到大循环j。

  不妨以下面这组数据为例可以找到区别:

  3 3
  1 100 1
  1 2
  1 3

  代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <vector>
 5 using namespace std;
 6 const int N = 100 + 5;
 7
 8 int n,k;
 9 int val[N];
10 vector<int> G[N];
11 int dp[N][N*2][2]; // 0 表示回到原地
12 void update(int & a,int b) {if(a < b) a = b;}
13 int dfs(int u,int fa)
14 {
15     for(int i=0;i<G[u].size();i++)
16     {
17         int v = G[u][i];
18         if(v == fa) continue;
19         dfs(v,u);
20         // j必须从大到小(?)
21         for(int j=k;j>=1;j--)
22         {
23             for(int t=1;t<=j;t++)
24             {
25                 // 最终都回到原地,那么左右都需要回到原地,此时需要多走两步 u->v & v->u
26                 if(t >= 2) update(dp[u][j][0], dp[u][j-t][0] + dp[v][t-2][0]);
27                 // 最终不用回到原地,有只要左边或者右边一种选择不用回到原地即可
28                 if(t >= 2) update(dp[u][j][1], dp[u][j-t][1] + dp[v][t-2][0]);
29                 update(dp[u][j][1], dp[u][j-t][0] + dp[v][t-1][1]);
30             }
31         }
32     }
33 }
34
35 int main()
36 {
37     while(scanf("%d%d",&n,&k) == 2)
38     {
39         memset(dp,0,sizeof dp);
40         for(int i=1;i<=n;i++)
41         {
42             G[i].clear();
43             scanf("%d",val+i);
44             for(int j=0;j<=k;j++) dp[i][j][0] = dp[i][j][1] = val[i];
45         }
46         for(int i=1;i<n;i++)
47         {
48             int u,v;
49             scanf("%d%d",&u,&v);
50             G[u].push_back(v);
51             G[v].push_back(u);
52         }
53         dfs(1, -1);
54         printf("%d\n",max(dp[1][k][0], dp[1][k][1]));
55     }
56     return 0;
57 }
时间: 2024-08-08 13:57:40

POJ 2486 Apple Tree ——(树型DP)的相关文章

POJ 2486 Apple Tree ( 树型DP )

#include <iostream> #include <cstring> #include <deque> using namespace std; #define SIZE 230 #define BACK 1 #define AWAY 0 int DP[SIZE][SIZE][2]; bool visits[SIZE]; int vals[SIZE]; deque< int > tree[SIZE]; int num, steps; void dfs

[Poj 2486] Apple Tree 树形DP

Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9069 Accepted: 3016 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

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

题意:给定一棵树图,一个人从点s出发,只能走K步,每个点都有一定数量的苹果,要求收集尽量多的苹果,输出最多苹果数. 思路: 既然是树,而且有限制k步,那么树形DP正好. 考虑1个点的情况:(1)可能在本子树结束第k步(2)可能经过了j步之后,又回到本节点(第k步不在本子树) 第二种比较简单,背包一下,就是枚举给本节点的孩子t多少步,收集到最多苹果数.第一种的话要求第k步终止于本节点下的某个子树中,那么只能在1个孩子子树中,所以应该是[其他孩子全部得走回来]+[本孩子不要求走回来]   or  

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

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7784   Accepted: 2603 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 amoun

POJ 3321 Apple Tree (树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21191   Accepted: 6436 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

POJ 2486 Apple Tree 树形DP+分组背包

链接:http://poj.org/problem?id=2486 题意:一棵(苹果)树,树上有N个结点(N<=100),起点是结点1.每个结点上有若干个苹果,我可以进行K步操作(K<=200),每次操作是从当前结点移动到相邻的结点,并且到了相邻的结点以后会吃掉上面的所有苹果并且苹果不再长出来,相邻是指两个结点之间有边相连.问在K步操作之后最多可以吃掉多少个苹果. 思路:刚入手的时候觉得是一般的树形背包问题,dp[i][j]代表的是以i为根的子树中走j个结点所能吃到的苹果数,来进行状态转移,但

POJ 2486 Apple Tree (树形dp)

这是一个树上的背包转移.注意要用dp[i][j][k]表示第i个节点用了j的路程是否回到i节点,k=0表示回到i点,k=1表示不回到i点.那么实际上就是树上的一个背包转移. #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <string> #include <cctype> #include

POJ 2486 Apple Tree

很好也很烦的一个树形DP,昨天搞了一晚上是在想不出,后来没办法去看题解了,完事发现非常令人感动的是,我一开始设的状态都错了,然后就一直错了下去,还好及时的回头是岸了. 不说废话了,正题: 题目大意:给一棵树,n个节点,每个节点有一个权值,要求从节点1出发最多走k步,求所经过的点的权值和的最大值,每个点经过一次后就变为0: 最近做了一些树DP 也小小的总结了点规律,树形dp一般是设dp[rt][j]即以rt为根的子树j状态.这个题可以设 dp[rt][j][0]表示以rt为根的子树走j步最后不回到

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 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