poj2486--Apple Tree(树状dp)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7789 | Accepted: 2606 |
Description
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
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
Sample Input
2 1
0 11
1 2
3 2
0 1 2
1 2
1 3
Sample Output
11
2
题目大意:给出一个n个节点的树,每一个节点上有个值,问不超过k步最高能够获得的值。i到j算一步,j到i也算一步
输入: 输入n和k。然后是n个节点的值,然后是n-1个i j代表了i和j节点相邻。
根是1.
非常easy看出来这是一个树状dp。dp[i][j]代表了以i节点为根。用j步能够得到的最大值,可是由于走到子树算是一步,走回到根也是一步,所以就要有两个dp关系,dp1[i][j]代表从i节点走j步又回到j节点的最大值,dp2[i][j]代表从i节点走j步不会到i节点的最大值。
那么状态转移方程为:当前节点为u。子树为v
回到i节点时:在节点u走j步,在子树v中走k步,从u到v和从v到u共走两步。那么在除v之外的其它子树走了j-k-2步。
dp1[u][j] = max(dp1[u][j],dp1[u][j-k-2]+dp1[v][k])
不回到i节点时:从节点u走j步
1.不在v子树中返回u,那么会在其它子树中返回u。在v中走k步。在u到v走一步,在除v外的子树走j-k-1步。
dp2[u][j] = max(dp2[u][j],dp1[u][j-k-1]+dp2[v][k])
2.在v子树中返回u,那么会在其它子树中存在不返回u的,在v中走k步。在u到v和v到u走两步。在除v之外的子树走j-k-2步。
dp2[u][j] = max(dp2[u][j],dp2[u][j-k-2]+dp1[v][k])
注意1:输入的节点i。j是相邻的关系。根是1。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std ;
struct tree{
int v , next ;
}edge[110];
int head[110] , cnt ;
int dp1[110][210] , dp2[110][210] ;//dp1返回。dp2不返回 dp[i][j]:从i节点出发使用j步能够得到的最大值
int c[110] , n , m ;
void add(int u,int v) {
edge[cnt].v = v ;
edge[cnt].next = head[u] ;
head[u] = cnt++ ;
return ;
}
void dfs(int u) {
int i , j , k , v ;
dp1[u][0] = dp2[u][0] = c[u] ;
if( head[u] == -1 )
return ;
for(i = head[u] ; i != -1 ; i = edge[i].next) {
v = edge[i].v ;
dfs(v) ;
}
for(i = head[u] ; i != -1 ; i = edge[i].next) {
v = edge[i].v ;
for(j = m ; j >= 0 ; j--) {
for(k = 0 ; k <= j ; k++) {
if( k+2 <= j ) {
dp1[u][j] = max(dp1[u][j],dp1[u][j-k-2]+dp1[v][k]) ;
dp2[u][j] = max(dp2[u][j],dp2[u][j-k-2]+dp1[v][k]) ;
}
if( k+1 <= j )
dp2[u][j] = max(dp2[u][j],dp1[u][j-k-1]+dp2[v][k]) ;
}
}
}
}
int Map[110][110] ;
queue <int> que ;
void bfs(int n) {
while( !que.empty() ) que.pop() ;
que.push(1) ;
int i , u , v ;
while( !que.empty() ) {
u = que.front() ;
que.pop() ;
for(i = 1 ; i <= n ; i++){
if( Map[u][i] == 1 ) {
Map[u][i] = Map[i][u] = 0 ;
add(u,i) ;
que.push(i) ;
}
}
}
}
int main() {
int i , j , u , v ;
while( scanf("%d %d", &n, &m) != EOF ) {
memset(head,-1,sizeof(head)) ;
memset(dp1,0,sizeof(dp1)) ;
memset(dp2,0,sizeof(dp2)) ;
memset(Map,0,sizeof(Map)) ;
cnt = 0 ;
for(i = 1 ; i <= n ; i++)
scanf("%d", &c[i]) ;
for(i = 1 ; i < n ; i++) {
scanf("%d %d", &u, &v) ;
Map[u][v] = Map[v][u] = 1 ;
}
bfs(n) ;
dfs(1) ;
int max1 = 0 ;
for(i = 0 ; i <= m ; i++) {
max1 = max(max1,dp2[1][i]) ;
}
printf("%d\n", max1) ;
}
return 0 ;
}
poj2486--Apple Tree(树状dp)的更多相关文章
- 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(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- E - Apple Tree(树状数组+DFS序)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
- 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 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
随机推荐
- C# Winform 模拟QQ新闻弹出框
一开始做的时候,觉得这个太简单了.真心做的时候还是遇到了不少的坑啊. 1)循环播放新闻内容,建议使用showdialog(),不要用show(),不太好控制前后之间的停顿. 2)窗口的初始位置为有下角 ...
- m_Orchestrate learning system---二十三、如何搜索概念图插件
m_Orchestrate learning system---二十三.如何搜索概念图插件 一.总结 一句话总结:要在百度你们搜索前端组件,前端组件 概念图工具,js概念图工具等等这些 用的话用go ...
- mysql语句判断一天操作记录的个数
话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下: 1 select ...
- rem自适应布局-移动端自适应必备:flexible.js
http://caibaojian.com/flexible-js.html
- [NOIP2012提高组]国王游戏
题目:洛谷P1080.Vijos P1779.codevs1198. 题目大意:国王和每个大臣左.右手各写了一个数.规定每个大臣得到的金币数为他前面所有人左手的数字的乘积除以他自己右手的数(向下取整) ...
- 紫书 习题 10-2 UVa 808(建立坐标+找规律)
这次是我遇见过最迷的一次 我写的程序uDebug全过 和ac程序对拍也过,求出来的坐标是一模一样的,最后结果输出的方式也是一样的 交上去就是错的 迷 第一次遇到这种情况 大佬在哪里 #include& ...
- 编译bash实现history的syslog日志记录
摘要: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1763145 一 ...
- 对GPDB查询计划的Motion结点的理解
GPDB在进行join查询时,可能会产生Motion结点 根据官方文档,总共有这几种Motion: redistribute 重分布(用hash取模的方法把join字段重分布到各个segment,相当 ...
- Trie树的常见应用大总结(面试+附代码实现)
(一)Trie的简单介绍 Trie树,又称字典树,单词查找树或者前缀树.是一种用于高速检索的多叉树结构,如英文字母的字典树是一个26叉树.数字的字典树是一个10叉树. 他的核心思想是空间换时间,空间消 ...
- generate the call load file
#!/usr/bin/perl -w $e911_call_percent = 0.0; $ims_node_number = 12; $local_ip = "10.86.52.2&quo ...