zoukankan      html  css  js  c++  java
  • poj 1947 Rebuilding Roads (树形 dp )

    http://poj.org/problem?id=1947

    Rebuilding Roads
    Time Limit: 1000MS
    Memory Limit: 30000K
    Total Submissions: 7061
    Accepted: 3092

    Description

    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

    Hint

    [A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]

    题意:
      一棵树 ,删除 是这棵树的的 p 个节点 成为 一颗独立的数 最少需要删除多少边
     
     

    一开始 。看到这个题不知到从何下手 ,看到 discuss 里面说到 树形 dp+ 背包
    自己想了一下 ,确实试着样 ;
     对于 一个根节点  ,我们可 以 从其子树 上 选 k 个 ,也可不选 ,这形成了 多重背包
     dp[r][k] 表示 以 r 为根的 树,留下 k 个 节点 要删除的最小边数(包括 根节点)
     
     
                 1: dp[r][k] + 1    不选 这棵子树
     dp[r][k] =
                2: dp[r][k - j] + dp[v][j]  v 为 r 的子树(进行 背包)
                
                注意:
                最少的边不一定 是以初始 的根为 跟的子树 ,所以要枚举 ,所有的 子树
                还有就是 ,除了 一开始 搜的 根之外 ,其他 的 更节点 都是 有 父节点的,所以 要  + 1

    View Code
     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<set>
     8 #include<map>
     9 #define Min(a,b)  a>b?b:a
    10 #define Max(a,b)  a>b?a:b
    11 #define CL(a,num)  memset(a,num,sizeof(a));
    12 #define inf 9999999
    13 #define maxn 160
    14 #define mod 100000000
    15 #define eps  1e-6
    16 #define ll long long
    17 using namespace std;
    18 vector<int> g[maxn];
    19 int dp[maxn][maxn] ;
    20 int  n ,m,in[maxn],vis[maxn];
    21 void dfs(int r)
    22 {
    23    if(vis[r]) return  ;
    24    vis[r] = 1;
    25 
    26    int i ,c,j,k;
    27    int sum = 0;
    28 
    29      /*for( i = 0 ; i < g[r].size();++i)
    30      {
    31          int v = g[r][i] ;
    32          if(!vis[v])  sum ++;
    33      }*/
    34    
    35     dp[r][1] = 0;//  这不是很懂,,为什么初始化 为 0;
    36 
    37 
    38     for( i = 0 ; i < g[r].size(); ++i)
    39     {
    40         int  j = g[r][i] ;
    41 
    42          if(vis[j]) continue ;
    43 
    44           dfs(j);
    45 
    46         for(c = m ; c >=0; --c)
    47         {
    48             int tmp = dp[r][c] + 1 ;
    49             for(k = 0 ; k <= c ; ++k)
    50             {
    51                 tmp = min(tmp,dp[j][k] + dp[r][c - k]);
    52             }
    53             dp[r][c] = tmp;
    54         }
    55 
    56     }
    57 
    58 }
    59 int main()
    60 {
    61     int i,x,y,root,j;
    62     while(scanf("%d %d",&n,&m)!=EOF)
    63     {
    64         for( i =0 ; i <= n; ++i)g[i].clear() ;
    65         for( i = 0 ; i < n - 1 ; ++i)
    66         {
    67             scanf("%d %d",&x,&y);
    68              g[x].push_back(y) ;
    69              g[y].push_back(x);
    70 
    71 
    72         }
    73       CL(vis,0);
    74 
    75       for( i = 0 ; i <= n; ++i)
    76       {
    77           for( j = 0; j <= m;++j)
    78               dp[i][j] = inf ;
    79       }
    80         dfs(1);
    81         int ans = dp[1][m];
    82 
    83         for( i = 1; i <= n ;++i)
    84         {
    85             if(ans > dp[i][m])
    86                ans = dp[i][m] + 1 ;//  除了 一开始 搜的 根之外 ,其他 的 更节点 都是 有 父节点的,所以 要  + 1
    87 
    88 
    89         }
    90         printf("%d\n",ans) ;
    91 
    92     }
    93 }

     

     

  • 相关阅读:
    Sql Server 存储过程删除一个表里(除ID外)完全重复的数据记录
    把一个库中的表复制到另外一个库的表中(Sql server 2005)
    ajax执行后台返回的提交表单及JS
    WinCE中使用本地数据库SQLite以及得到当前应用程序所在路径
    如何评测一个软件工程师的计算机网络知识水平与网络编程技能水平
    如何评测软件工程知识技能水平?
    深入理解TCP协议及其源代码
    Socket与系统调用深度分析
    创新产品的需求分析:未来的图书会是什么样子?
    ubuntu小问题集合
  • 原文地址:https://www.cnblogs.com/acSzz/p/2637392.html
Copyright © 2011-2022 走看看