版权声明:本文为博主原创文章,未经博主允许不得转载。

Rebuilding Roads
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8227   Accepted: 3672

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.] 

Source

题意:

给你一棵树,求最少剪掉几条边使能够得到一个p个节点的树。

思路:

用dp[i][j]代表以i为根的子树要变成j个节点需要剪掉的边数。

考虑子节点的时候不用考虑它与父节点的那条边,就当不存在,那么最后求的的最小边数加1就行了。
考虑一个节点时,有两种选择,要么剪掉跟子节点相连的边,则dp[i][j] = dp[i][j]+1;

要么不剪掉,则dp[i][j] = max(dp[i][j], dp[i][k]+dp[son][j-k]);

然后随便以一条边dfs求解就行了。

为什么可以这样求解呢。

对于树上的一个节点。要么在待求解子树中。要么不在。在的话即dp[i][p]中。不然的话就肯定在其它子树上了。

详细见代码:

  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. using namespace std;
  5. const int maxn=250;
  6. const int INF=0x3f3f3f3f;
  7. struct node
  8. {
  9. int v;
  10. node *next;
  11. } edge[maxn<<1],*head[maxn];
  12. int n,m,cnt,dp[maxn][maxn];
  13. void adde(int u,int v)
  14. {
  15. edge[cnt].v=v;
  16. edge[cnt].next=head[u];
  17. head[u]=&edge[cnt++];
  18. }
  19. void dfs(int fa,int u)
  20. {
  21. int i,j,v,mm=INF;
  22. node *p;
  23. dp[u][1]=0;
  24. for(p=head[u];p!=NULL;p=p->next)
  25. {
  26. v=p->v;
  27. if(v==fa)
  28. continue;
  29. dfs(u,v);
  30. for(i=m;i>=1;i--)//01背包。从大到小装
  31. {
  32. mm=dp[u][i]+1;//不要该子树.
  33. for(j=1;j<i;j++)//要该子树
  34. mm=min(mm,dp[u][j]+dp[v][i-j]);
  35. dp[u][i]=mm;
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41. int i,u,v,ans;
  42. while(~scanf("%d%d",&n,&m))
  43. {
  44. cnt=0;
  45. memset(head,0,sizeof head);
  46. memset(dp,0x3f,sizeof dp);
  47. for(i=1;i<n;i++)
  48. {
  49. scanf("%d%d",&u,&v);
  50. adde(u,v);
  51. adde(v,u);
  52. }
  53. dfs(1,1);
  54. ans=dp[1][m];
  55. for(i=1;i<=n;i++)
  56. ans=min(ans,dp[i][m]+1);//加1是因为根转移了
  57. printf("%d\n",ans);
  58. }
  59. return 0;
  60. }

DP Intro - poj 1947 Rebuilding Roads(树形DP)的更多相关文章

  1. POJ 1947 Rebuilding Roads 树形DP

    Rebuilding Roads   Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...

  2. POJ 1947 Rebuilding Roads 树形dp 难度:2

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9105   Accepted: 4122 ...

  3. DP Intro - poj 1947 Rebuilding Roads

    算法: dp[i][j]表示以i为根的子树要变成有j个节点的状态需要减掉的边数. 考虑状态转移的时候不考虑i的父亲节点,就当不存在.最后统计最少减去边数的 时候+1. 考虑一个节点时,有两种选择,要么 ...

  4. 树形dp(poj 1947 Rebuilding Roads )

    题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...

  5. POJ 1947 Rebuilding Road(树形DP)

    Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...

  6. [poj 1947] Rebuilding Roads 树形DP

    Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...

  7. POJ 1947 Rebuilding Roads (树dp + 背包思想)

    题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...

  8. POJ1947 - Rebuilding Roads(树形DP)

    题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

随机推荐

  1. Spark 0.9.1和Shark 0.9.1分布式安装指南

    目录 目录 1 1. 约定 1 2. 安装Scala 1 2.1. 下载 2 2.2. 安装 2 2.3. 设置环境变量 2 3. 安装Spark 2 3.1. 部署 2 3.2. 下载 3 3.3. ...

  2. linux vi vim文本编辑器

    vim是vi的加强版,建议使用vim. vim拥有三种模式: 命令模式(常规模式) vim启动后,默认进入命令模式,任何模式都可以通过esc键来回到命令模式.命令模式可以通过键入不同的命令来完成选择, ...

  3. Linq转换操作之ToArray,ToList,ToDictionary源码分析

    Linq转换操作之ToArray,ToList,ToDictionary源码分析 一:linq中的转换运算符 1. ToArray 我们经常用在linq查询上吧. linq只能运用在IEnumerab ...

  4. js和C# 时间日期格式转换

    下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...

  5. Hadoop 文件命令

    * 文件操作 * 查看目录文件 * $ hadoop dfs -ls /user/cl * * 创建文件目录 * $ hadoop dfs -mkdir /user/cl/temp * * 删除文件  ...

  6. 在 CentOS 上运行 ZKEACMS

    ZKEACMS Core 是基于 .net core 开发的,可以在 windows, linux, mac 上跨平台运行,接下来我们来看看如何在 CentOS 上运行 ZKEACMS. 安装 .Ne ...

  7. Redis协议规范(译文)

    Redis客户端使用名为RESP(Redis序列化协议)的协议与Redis服务器进行通信. 虽然该协议是专为Redis设计的,但它可以用于其他CS软件项目的通讯协议. RESP是以下几方面的考虑: 易 ...

  8. python IDE安装-mac

    mac 配置Python集成开发环境(Eclipse +Python+Pydev)   1.下载Mac版64位的Eclipse. 进入到Eclipse官方网站的下载页面(http://www.ecli ...

  9. 洛谷P4931 情侣?给我烧了!(加强版)(组合数学)

    题面 传送门 题解 首先我们算出刚好有\(k\)对情侣的方案数 从\(n\)对情侣中选出\(k\)对,方案数为\({n\choose k}\) 从\(n\)排座位中选出\(k\)排,方案数为\({n\ ...

  10. [bzoj2816][ZJOI2012]网络(LCT,splay)

    传送门 题解 话说以前还真没见过用LCT只维护一条链的……好像除了树点涂色那题…… 先看一下题目规定的两个性质 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜 ...