1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 #define inf 10000000 7 int s[160],p[160],v[160],dp[160][160],n,m; 8 void check(int root) 9 { 10 for(int i=0;i<=m;i++) dp[root][i] = inf; 11 dp[root][1]=0; 12 int tem = s[root]; 13 while(tem){ 14 check(tem); 15 for(int i=m;i>=0;--i){ 16 int temm = dp[root][i]+1; 17 for(int j=0;j<=i;++j){ 18 temm = min(temm,dp[tem][i-j]+dp[root][j]); 19 } 20 dp[root][i] = temm; 21 } 22 tem = p[tem]; 23 } 24 } 25 int solve(int root) 26 { 27 check(root); 28 int ans = dp[root][m]; 29 for(int i=1;i<=n;i++){ 30 ans = min(ans,dp[i][m]+1); 31 } 32 return ans; 33 } 34 int main() 35 { 36 int a,b,root; 37 cin>> n>> m; 38 memset(s,0,sizeof(s)); 39 memset(v,0,sizeof(v)); 40 for(int i=0;i<n-1;i++) 41 { 42 cin>> a>> b; 43 p[b]=s[a]; 44 s[a]=b; 45 v[b]=1; 46 } 47 for(int i=1;i<=n;i++) if(!v[i]) root=i; 48 cout<< solve(root)<< endl; 49 return 0; 50 }
~\Desktop\1947.cpp.html
转载于:https://www.cnblogs.com/eth0/archive/2011/05/28/2061019.html