hdu trilogy 2 Rebuilding Roads

The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn.  Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do.  He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
 
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J.  Node I is node J's parent in the tree of roads.
 
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
 
Sample Input
11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11
 
Sample Output
2
********************************************************************************************
Dp tree tree 01 backpack
*******************************************************************************************89
1  / * 
2  The title of the DP [s] [i] [j]:
 . 3    represents the time in s left subtree of the root of the i-th minimum cost required to son nodes j
 4  * / 
5 # the include <the iostream>
 . 6 #include < String >
 . 7 #include <CString>
 . 8 #include <Queue>
 . 9 #include <cstdio>
 10  the using  namespace STD;
 . 11  #define INF 0x3f3f3f3f
 12 is  struct Node
 13 is  {
 14      int V, Next;
 15 E} [ 1001 ];
 16  int head [ 1001 ], the I;
17 int dp[181][181][181];
18 int ch[1001],n,p,i;
19 int a,b,res;
20 int min(int a,int b)
21   {
22       return a<b?a:b;
23   }
24 void add(int a,int b)
25   {
26      e[I].v=b;
27      e[I].next=head[a];
28      head[a]=I++;
 29    }
 30  void DFS ( int s)
 31 is  {
 32       IF (head [s] == - . 1 )
 33 is       {
 34 is           DP [s] [ 0 ] [ 0 ] = . 1 ; // expressed in s root subtree If not only takes a cut edge 
35           DP [S] [ 0 ] [ . 1 ] = 0 ; // If you have left, without reducing edge; 
36       }
 37 [       the else 
38 is        {
 39            DP [S] [ 0 ] [ 1 ] = 0; // If you have left, without reducing edge; 
40            DP [s] [CH [s]] [ 0 ] = . 1 ; // represents a side cut only when s is the root subtree if not taken 
41            int m;
 42 is            // the following are enumerated find optimal value per each stage of the backpack 01, (tree) 
43 is            for ( int IT head = [S], K = . 1 ; IT = -! . 1 ; IT E = [IT] .next, K ++ )
 44 is             {
 45                 DFS (E [IT] .v);
 46 is                 for ( int  IS = . 1 ; IS <= P; IS ++ )
 47                  for (int m=0;m<is;m++)
48                  dp[s][k][is]=min(dp[s][k][is],dp[s][k-1][is-m]+dp[e[it].v][ch[e[it].v]][m]);
49            }
50       }
51       if(dp[s][ch[s]][p]!=INF)
52        {
53            if(s!=1)//不是root加1,
54             res=min(res,dp[s][ch[s]][p]+1);
55            else
56              res=min(res,dp[s][ch[s]][p]);
57        }
58  }
59  int main()
60  {
61      cin>>n>>p;
62      memset(dp,0x3f,sizeof(dp));
63      memset(head,-1,sizeof(head));
64      memset(ch,0,sizeof(ch));
65      I=0;
66      res=INF;
67      if(n==1)
68       printf("0\n");
69      else
70        {
71            for(i=1;i<n;i++)
72             {
73                 cin>>a>>b;
74                 add(a,b);
75                 ++ch[a];//记录儿子
76             }
77             dfs(1);
78         printf("%d\n",res);
79        }
80        return 0;
81  }
View Code

adhere to! ! ! ! ! !

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3343563.html

Guess you like

Origin blog.csdn.net/weixin_34109408/article/details/93432940