标签:
11 6 1 2 1 3 1 4 1 5 2 6 2 7 2 8 4 9 4 10 4 11
2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 const int maxn = 200; 7 int n,k,ret,dp[maxn][maxn],son[maxn]; 8 vector<int>g[maxn]; 9 int dfs(int u){ 10 son[u] = 1; 11 for(int i = g[u].size()-1; i >= 0; --i) son[u] += dfs(g[u][i]); 12 dp[u][1] = g[u].size();//u有g[u].size()个直系儿子 删除儿子还剩自己 13 for(int i = g[u].size()-1; i >= 0; --i){ 14 for(int j = son[u]; j > 0; --j) 15 for(int k = 1; k < j && k <= son[g[u][i]]; ++k) 16 dp[u][j] = min(dp[u][j],dp[u][j - k] + dp[g[u][i]][k] - 1); 17 } 18 if(son[u] >= k) ret = min(ret,dp[u][k] + (u != 1)); 19 return son[u]; 20 } 21 int main(){ 22 int u,v; 23 while(~scanf("%d%d",&n,&k)){ 24 for(int i = 0; i < maxn; ++i) g[i].clear(); 25 for(int i = 1; i < n; ++i){ 26 scanf("%d%d",&u,&v); 27 g[u].push_back(v); 28 } 29 memset(dp,0x3f,sizeof dp); 30 memset(&ret,0x3f,sizeof ret); 31 dfs(1); 32 printf("%d\n",ret); 33 } 34 return 0; 35 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4789893.html