DP Intro - poj 1947 Rebuilding Roads(树形DP)
版权声明:本文为博主原创文章,未经博主允许不得转载。
Rebuilding Roads
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 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]中。不然的话就肯定在其它子树上了。
详细见代码:
- #include <iostream>
- #include<stdio.h>
- #include<string.h>
- using namespace std;
- const int maxn=250;
- const int INF=0x3f3f3f3f;
- struct node
- {
- int v;
- node *next;
- } edge[maxn<<1],*head[maxn];
- int n,m,cnt,dp[maxn][maxn];
- void adde(int u,int v)
- {
- edge[cnt].v=v;
- edge[cnt].next=head[u];
- head[u]=&edge[cnt++];
- }
- void dfs(int fa,int u)
- {
- int i,j,v,mm=INF;
- node *p;
- dp[u][1]=0;
- for(p=head[u];p!=NULL;p=p->next)
- {
- v=p->v;
- if(v==fa)
- continue;
- dfs(u,v);
- for(i=m;i>=1;i--)//01背包。从大到小装
- {
- mm=dp[u][i]+1;//不要该子树.
- for(j=1;j<i;j++)//要该子树
- mm=min(mm,dp[u][j]+dp[v][i-j]);
- dp[u][i]=mm;
- }
- }
- }
- int main()
- {
- int i,u,v,ans;
- while(~scanf("%d%d",&n,&m))
- {
- cnt=0;
- memset(head,0,sizeof head);
- memset(dp,0x3f,sizeof dp);
- for(i=1;i<n;i++)
- {
- scanf("%d%d",&u,&v);
- adde(u,v);
- adde(v,u);
- }
- dfs(1,1);
- ans=dp[1][m];
- for(i=1;i<=n;i++)
- ans=min(ans,dp[i][m]+1);//加1是因为根转移了
- printf("%d\n",ans);
- }
- return 0;
- }
DP Intro - poj 1947 Rebuilding Roads(树形DP)的更多相关文章
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- POJ 1947 Rebuilding Roads 树形dp 难度:2
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9105 Accepted: 4122 ...
- DP Intro - poj 1947 Rebuilding Roads
算法: dp[i][j]表示以i为根的子树要变成有j个节点的状态需要减掉的边数. 考虑状态转移的时候不考虑i的父亲节点,就当不存在.最后统计最少减去边数的 时候+1. 考虑一个节点时,有两种选择,要么 ...
- 树形dp(poj 1947 Rebuilding Roads )
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...
- POJ 1947 Rebuilding Road(树形DP)
Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, n ...
- [poj 1947] Rebuilding Roads 树形DP
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...
- POJ1947 - Rebuilding Roads(树形DP)
题目大意 给定一棵n个结点的树,问最少需要删除多少条边使得某棵子树的结点个数为p 题解 很经典的树形DP~~~直接上方程吧 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v] ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
随机推荐
- 完全卸载memcached的方法(CentOS)
前阵子给服务器装了个memcached,4G的内存,想给网站提提速,实际上不但没有明显效果,反倒耗费内存,看着碍眼,于是想卸载,网上各种搜索+自己实践,搞出一个傻瓜方案来: 1.首先查找你的memca ...
- Android 控件在布局中按比例放置[转]
转自:http://netsky1990.blog.51cto.com/2220666/997452 在Android开发中常用到线性布局LinearLayout对界面进行具体的创建,其中 ...
- Domino 迁移到Exchange 之 Domino Admin 安装!
双击Setup 安装:
- U盘安装Centos6.2
原文地址:http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0819/8452.html. 第一步:制作系统U盘(略). 第二步:设置BI ...
- [转]java设计模式
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- 基于JSP+Servlet开发手机销售购物商城系统(前台+后台)源码
基于JSP+Servlet开发手机销售购物商城系统(前台+后台) 开发环境: Windows操作系统 开发工具:Eclipse/MyEclipse+Jdk+Tomcat+MySQL数据库 运行效果图: ...
- IIS 发布webservice 需要用户名和密码访问 解决
今天,我在IIS上发布了一个自己写的webservice,然后我在远程通过浏览器来访问这个webservice的时候出现一个登录界面如下 之前我朋友发布webservice的时候也出现过一次,那次好 ...
- WinForm中DataGridView的使用(四) - 区分单双击事件
虽然DataGridView单双击事件都有,但双击事件其实也会触发单击事件的处理,所以如果双击事件和单击事件的行为不同,或者双击时不想触发单击事件,或者单击事件会阻塞双击事件的处理时(比如单击后会有弹 ...
- Web Server 在IIS上部署ASP.NET Core项目
在IIS上部署ASP.NET Core项目 一.配置应用程序池为无托管: 二.安装ASPNETCoreModule:(核心) 下载地址:https://go.microsoft.com/fwlink/ ...
- kali linux之Bdfproxy
Bdfproxy(mitmproxy) 基于流量劫持动态注入shellcode(arp欺骗,dns欺骗,流氓ap) 步骤: 开启流量转发---------sysctl -w net.ipv4.ip_f ...