zoukankan      html  css  js  c++  java
  • hdu 三部曲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    树上的01背包
    *******************************************************************************************89
     1 /*
     2 本题中的dp[s][i][j]:
     3   表示以s为root的子树的到第i个儿子时留下j个结点所需的最小代价
     4 */
     5 #include<iostream>
     6 #include<string>
     7 #include<cstring>
     8 #include<queue>
     9 #include<cstdio>
    10 using namespace std;
    11 #define INF  0x3f3f3f3f
    12 struct node
    13 {
    14     int v,next;
    15 }e[1001];
    16 int head[1001],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  {
    32      if(head[s]==-1)
    33      {
    34          dp[s][0][0]=1;//表示以s为root的子树如果都不取时只需剪掉一个边
    35          dp[s][0][1]=0;//如果都留下,无需减边;
    36      }
    37      else
    38       {
    39           dp[s][0][1]=0;//如果都留下,无需减边;
    40           dp[s][ch[s]][0]=1;//表示以s为root的子树如果都不取时只需剪掉一个边
    41           int m;
    42           //下面是每次枚举求每个阶段的最优值01背包,(树上的)
    43           for(int it=head[s],k=1;it!=-1;it=e[it].next,k++)
    44            {
    45                dfs(e[it].v);
    46                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
    ");
    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
    ",res);
    79        }
    80        return 0;
    81  }
    View Code

    坚持!!!!!!

  • 相关阅读:
    CSS3 不定宽高水平垂直居中
    css 标题三条横线
    浏览器工作原理
    JS---原型继承和多重继承
    你在工作中无意间会犯的四个错误
    CNPC海外操作人员英语日常用语900句
    惊呆了!无聊感可激发创造力
    求职简历这些错你犯过吗?
    研究:低智商男人易出轨
    TED:6个月学会1门外语
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3343563.html
Copyright © 2011-2022 走看看