【树形dp】Apple Tree
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10800 | Accepted: 3629 |
Description
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
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,V;
vector<int> vec[301];
int dp[301][301][2];
int a[301]; void dfs(int x,int fa){
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]==fa) continue;
dfs(vec[x][i],x);
int SON=vec[x][i];
for(int v=V;v>=1;v--){
for(int k=1;k<=v;k++){
dp[x][v][0]=max(dp[x][v][0],dp[x][v-k][1]+dp[SON][k-1][0]);//从别的子树兜了一圈到这可子树,不回去了
dp[x][v][0]=max(dp[x][v][0],dp[x][v-k][0]+dp[SON][k-2][1]);//从这颗子树兜了一圈到别的子树,不回去了
dp[x][v][1]=max(dp[x][v][1],dp[x][v-k][1]+dp[SON][k-2][1]);//从别的子树兜了一圈到这可子树然后还回去
}
}
}
}
int ans; int main(){
while(scanf("%d%d",&N,&V)!=EOF){
for(int i=1;i<=300;i++) vec[i].clear();
memset(dp,0,sizeof(dp));
ans=0;
for(int i=1;i<=N;i++) a[i]=read();
if(N==1){//注意要特判
printf("%d\n",a[1]);
continue;
}
for(int i=1;i<N;i++){
int u=read(),v=read();
vec[u].push_back(v);
vec[v].push_back(u);
}
for(int i=1;i<=N;i++)
for(int j=0;j<=V;j++){
dp[i][j][0]=dp[i][j][1]=a[i];
}
dfs(1,0);
printf("%d\n",max(dp[1][V][0],dp[1][V][1]));
}
}
【树形dp】Apple Tree的更多相关文章
- POJ 2486 树形背包DP Apple Tree
设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...
- HDU 5379 树形DP Mahjong tree
任意一棵子树上节点的编号连续,每个节点的所有二字节点连续,求编号方案的总数. 稍微分析一下可知 每个节点的非叶子节点个数不能多于两个,否则这个子树无解,从而整棵树都无解. 每棵子树将所有节点按照编号从 ...
- CodeForces 109C 树形DP Lucky Tree
赶脚官方题解写得挺清楚的说,=_= 注意数据范围用long long,否则会溢出. #include <iostream> #include <cstdio> #include ...
- 【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 状态方程有点难想)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9808 Accepted: 3260 Descri ...
- URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...
- POJ 2486 Apple Tree(树形DP)
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...
- ural 1018 Binary Apple Tree(树形dp | 经典)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
随机推荐
- 阿里云服务器下安装配置 vsftpd —— 基于CentOS 6.3 【简洁版】
原文链接:http://www.tuicool.com/articles/nuiQBja 1.更新yum源 我是直接 yum update 更新的 2.安装vsftp 使用yum命令安装vsftpd ...
- dokuwiki安装部署
dokuwiki的地址:https://www.dokuwiki.org/dokuwiki# 1.部署dokuwiki 在D:\xampp\htdocs(xampp安装目录)新建一个doku文件夹,把 ...
- python设计模式之单例模式(一)
单例设计模式的概念: 单例设计模式即确保类有且只有一个特定类型的对象,并提供全局访问点.一般我们操作数据库的时候为了避免统一资源产生互相冲突,创建单例模式可以维护数据的唯一性. 单例模式的特性: 确保 ...
- 微信小程序的下拉刷新
微信小程序的下拉刷新:在page的js文件中有监听用户下拉刷新的处理函数onPullDownRefresh:function(){} //js文件中自带的处理函数,在onUnload下面,注意不要重复 ...
- docker安装(2016-08-25版本)
. 通过命令对系统的版本进行查看 [root@localhost ~]# uname -a [root@localhost ~]# cat /etc/issue --> 如果是6.5之前的版本 ...
- 使用Storm实现实时大数据分析(转)
原文链接:http://blog.csdn.net/hguisu/article/details/8454368 简单和明了,Storm让大数据分析变得轻松加愉快. 当今世界,公司的日常运营经常会生成 ...
- PostGIS pgrouting路径分析
--让数据库支持PostGIS和pgRouting的函数和基础表(安装后第一次使用时执行,以后都不再执行) CREATE EXTENSION postgis; CREATE EXTENSION pgr ...
- mui页面跳转
$('.mui-title').on('click',function(){ mui.openWindow({ //跳转到指导信息页面 url:"/index.php?m=mobile&am ...
- hdu 2768(建图,最大点独立集)
Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { TreeLinkNode ...