标签:
2 1 0 11 1 2 3 2 0 1 2 1 2 1 3
11 2
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 const int maxn = 200; 7 vector<int>g[maxn]; 8 int dp[maxn][maxn][2],w[maxn],n,K; 9 void dfs(int u,int fa) { 10 for(int i = 0; i <= K; ++i) 11 dp[u][i][0] = dp[u][i][1] = w[u]; 12 for(int i = g[u].size()-1; i >= 0; --i) { 13 if(g[u][i] == fa) continue; 14 dfs(g[u][i],u); 15 for(int j = K; j > 0; --j) { 16 for(int k = 1; k <= j; ++k) { 17 dp[u][j][1] = max(dp[u][j][1],dp[u][j - k][0] + dp[g[u][i]][k - 1][1]); 18 if(k >= 2) { 19 dp[u][j][1] = max(dp[u][j][1],dp[u][j-k][1] + dp[g[u][i]][k-2][0]); 20 dp[u][j][0] = max(dp[u][j][0],dp[u][j - k][0] + dp[g[u][i]][k - 2][0]); 21 } 22 } 23 } 24 } 25 } 26 int main() { 27 int u,v; 28 while(~scanf("%d%d",&n,&K)) { 29 for(int i = 1; i <= n; ++i) scanf("%d",w+i); 30 for(int i = 0; i < maxn; ++i) g[i].clear(); 31 for(int i = 1; i < n; ++i) { 32 scanf("%d%d",&u,&v); 33 g[u].push_back(v); 34 g[v].push_back(u); 35 } 36 memset(dp,0,sizeof dp); 37 dfs(1,-1); 38 printf("%d\n",max(dp[1][K][0],dp[1][K][1])); 39 } 40 return 0; 41 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4799454.html