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]中。不然的话就肯定在其它子树上了。
详细见代码:
#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;
}
分享到:
相关推荐
北大POJ1724-ROADS 解题报告+AC代码
POJ1724-ROADS 测试数据。数据来源:CEOI 1998 Round II
poj 2749 Building roads.md
北大POJ3411-Paid Roads【class】 解题报告+AC代码
关于poj dp分类,我一直寻找dp的分类,终于找到了,也上传一下
Poj 3342 这是一道树状dp题目,题意是这样的,一个公司,每个人有且仅有一个Boss,除了最大的Boss没有Boss(显然),现在要参加一个聚会,要求参加的人数最多,但是棘手的.......
poj2342,树形dp,dp[i][0]表示i不参加party,其下属(包括非直接下属)能得到的最优值。dp[i][1]表示i参加的其下属和他能得到的最优值。
NULL 博文链接:https://200830740306.iteye.com/blog/603493
这是一道很不错的题目,dp解决的经典例子,是学习dp,和练习的好题目。。。。
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
北大POJ1015-Jury Compromise【动态规划DP】 解题报告+AC代码
1.把自己的污水排到河里V 2.把自己的污水送到右边> 3.把自己的污水送到左边
这是黑不来就的poj上的所有dp题目的大型分类 好使好用
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
poj 2763 程序 线段树 LCA 2000MS AC
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
NULL 博文链接:https://128kj.iteye.com/blog/1705139
2遍dp poj_3613解题报告 poj_3613解题报告
poj分类poj分类poj分类poj分类
POJ题解 个人写法 线段树每个人都不一样