标签:des style blog os strong io for 2014
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7120 | Accepted: 2370 |
Description
Input
Output
Sample Input
2 1 0 11 1 2 3 2 0 1 2 1 2 1 3
Sample Output
11 2
Source
虽然这个题目是看的解题报告,但是还是终于过了。这题真是难为了我very very very long long time time
dp[a][b][0]:节点a 走b 步回到节点a
dp[a][b][1]: 节点a 走 b 步不回到节点a
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <cstdlib> #include <queue> #include <set> #define N 210 #define M 110 using namespace std; struct num { int x,y,next; }a[2*N]; int b[N],Top,n,m; int dp[N][N][2],temp[N][N][2]; int main() { //freopen("data.txt","r",stdin); void addeage(int x,int y); void dfs(int u,int fa); while(scanf("%d %d",&n,&m)!=EOF) { memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); dp[i][0][0] = x; dp[i][0][1] = x; } memset(b,-1,sizeof(b)); Top = 0; for(int i=1;i<=n-1;i++) { int x,y; scanf("%d %d",&x,&y); addeage(x,y); addeage(y,x); } dfs(1,-1); int ans = 0; for(int i=0;i<=m;i++) { ans = max(ans,dp[1][i][0]); } printf("%d\n",ans); } return 0; } void addeage(int x,int y) { a[Top].y = y; a[Top].next = b[x]; b[x] = Top++; } void dfs(int u,int fa) { for(int i=b[u];i!=-1;i=a[i].next) { int v = a[i].y; if(v==fa) { continue; } dfs(v,u); for(int i=1;i<=n;i++) { for(int j=0;j<=m;j++) { temp[i][j][0] = dp[i][j][0]; temp[i][j][1] = dp[i][j][1]; } } for(int i=1;i<=m;i++) { for(int j=1;j<=i;j++) { dp[u][i][0] = max(dp[u][i][0],temp[u][i-j][1]+temp[v][j-1][0]); if(j>=2) { dp[u][i][0] = max(dp[u][i][0],temp[v][j-2][1]+temp[u][i-j][0]); dp[u][i][1] = max(dp[u][i][1],temp[u][i-j][1]+temp[v][j-2][1]); } } } } }
POJ 2486 Apple Tree,布布扣,bubuko.com
标签:des style blog os strong io for 2014
原文地址:http://blog.csdn.net/yongxingao/article/details/38375815