标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9989 | Accepted: 3324 |
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
// // main.cpp // poj2486 // // Created by Candy on 9/27/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N=1e2+5,K=2e2+5,INF=1e9+5; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x; } int n=0,k,u,v,w[N]; struct edge{ int v,ne; }e[N<<1]; int h[N],cnt=0; inline void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt; } int d[N][K][2]; void dp(int u,int fa){ for(int i=0;i<=k;i++)d[u][i][0]=d[u][i][1]=w[u]; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(v==fa) continue; dp(v,u); for(int j=k;j>=1;j--){ for(int z=1;z<=j;z++){ if(z>=2) d[u][j][0]=max(d[u][j][0],d[u][j-z][0]+d[v][z-2][0]); if(z>=1) d[u][j][1]=max(d[u][j][1],d[u][j-z][0]+d[v][z-1][1]); if(z>=2) d[u][j][1]=max(d[u][j][1],d[u][j-z][1]+d[v][z-2][0]); } //printf("d %d %d %d %d\n",u,j,d[u][j][0],d[u][j][1]); } } } int main(){ while(scanf("%d%d",&n,&k)!=EOF){ cnt=0;memset(h,0,sizeof(h)); for(int i=1;i<=n;i++) w[i]=read(); for(int i=1;i<=n-1;i++){u=read();v=read();ins(u,v);} //memset(f,0,sizeof(f)); dp(1,-1); printf("%d\n",d[1][k][1]); // cout<<"\n\n"; // for(int i=1;i<=n;i++ ) printf("son %d %d\n",i,son[i]); } }
标签:
原文地址:http://www.cnblogs.com/candy99/p/5912399.html