POJ Apple Tree 树形dp
2017-10-26 19:41
441 查看
额。之前我不是发了一道大逃杀的题吗,如果觉得实在不懂可以先做这道。
Language: Default Apple Tree
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; } } |
相关文章推荐
- poj 2486 Apple Tree (树形背包dp)
- Apple Tree POJ - 2486 树形dp
- POJ 1848 Tree 树形DP
- POJ 1741 Tree 树形DP(分治)
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
- codeforces 461B B. Appleman and Tree(树形dp)
- codeforces 462 d Appleman and Tree(树形dp)
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- Appleman and Tree - CodeForces 461 B 树形dp
- poj 1848 Tree 树形dp
- poj 1848 Tree(树形DP,太难了,三种状态,四种状态转换)
- Ural_1018. Binary Apple Tree(树形DP)
- CF 461B - Appleman and Tree(树形DP)
- 【POJ 1848】Tree【树形DP】
- poj&nbsp;2486&nbsp;Apple&nbsp;Tree&nbsp;tree&nbsp;dp
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- CF 462D(Appleman and Tree-树形dp)
- URAL 1018. Binary Apple Tree(树形DP)
- Codeforces Round #263 (Div. 1) B. Appleman and Tree( 树形DP )
- Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)