您的位置:首页 > 移动开发

POJ Apple Tree 树形dp

2017-10-26 19:41 441 查看
    额。之前我不是发了一道大逃杀的题吗,如果觉得实在不懂可以先做这道。
Language:
Default

Apple Tree

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 11115Accepted: 3739
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. H
a198
e 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

Source
   
 分析:之前有道大逃杀的题是没有告诉你根的,而这一道是以1为根,故而要简单一点,少了2的状态,分析还是和大逃杀一样,可以戳连接:http://blog.csdn.net/qq_37321744/article/details/78357522;
  # include <iostream>
# include <cstdio>
# include <cstring>
# include <cmath>
# include <list>
# include <map>
# include <queue>
# include <algorithm>
using namespace std;
typedef long long ll;
int read()
{
int f=1,i=0;char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
i=(i<<3)+(i<<1)+ch-'0';
ch=getchar();
}
return f*i;
}
struct node
{
int v,next,w;
}E[205<<2];
int n,m,x,y,cnt,f[205][205][2];
int w[205],first[205],tmp[205];
bool vis[205];
inline void initial()
{
cnt=0;
memset(first,0,sizeof(first));
memset(f,0,sizeof(f));
memset(vis,0,sizeof(vis));
}
inline void AddEdge(int x,int y)
{
E[++cnt].next=first[x];
first[x]=cnt;E[cnt].v=y;
}
inline void DFS(int u)
{
vis[u]=true;
for(int i=0;i<=m;++i) f[u][i][0]=f[u][i][1]=w[u];
for(int i=first[u];i;i=E[i].next)
{
int v=E[i].v;
if(!vis[v])
{
DFS(v);
for(int i=0;i<=m;++i) tmp[i]=f[u][i][1];
for(int j=0;j<=m;++j)
{
for(int i=j+1;i<=m;++i)
tmp[i]=max(tmp[i],f[u][i-j-1][0]+f[v][j][1]);
for(int i=j+2;i<=m;++i)
tmp[i]=max(tmp[i],f[u][i-j-2][1]+f[v][j][0]);
}
for(int i=0;i<=m;++i) f[u][i][1]=tmp[i];

for(int i=0;i<=m;++i) tmp[i]=f[u][i][0];
for(int j=0;j<=m;++j)
{
for(int i=j+2;i<=m;++i)
tmp[i]=max(tmp[i],f[u][i-j-2][0]+f[v][j][0]);
}
for(int i=0;i<=m;++i) f[u][i][0]=tmp[i];
}
}
}
int main()
{

while(scanf("%d%d",&n,&m)!=EOF)
{
initial();
for(int i=1;i<=n;++i) w[i]=read();
for(int i=1;i<n;++i)
{
x=read(),y=read();
AddEdge(x,y),AddEdge(y,x);
}
DFS(1);int ans=0;
for(int i=0;i<=m;++i)
for(int j=0;j<2;++j) ans=max(ans,f[1][i][j]);
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: