POJ 1947 Rebuilding Roads 树形DP(背包)

原题:


Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 8567 Accepted: 3836

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个点。


一道比较基础的树形背包,由于最后的结果一定是以某个结点为根的树干部分

定义状态: dp[i][j]表示在以结点i为根的树中,分配j个结点的指标,所需要删除的最少边【一定包含第i个结点】

p[u][j]= min(dp[u][j],dp[son][k]+dp[u][j-k]);

表明 给结点u分配j的容量, u再把这j个分配给他的各个儿子结点,给当前儿子结点分配k个容量,加上 之前遍历过的所有儿子结点中分配j-k个容量,看这个是否更优。

注意这里 j是从大往小的遍历,利用背包缩小空间维度的优化方法,这里右边min函数中的两个dp[u][]表示的是上一轮中扫过结点u中从0到I-1个儿子结点所得到的dp值,而左边的dp[u][j]表示的是包括的这次第i个儿子结点后的dp值


另外就是要注意循环的边界条件,每次根节点必占一个容量。

初始化时,dp[u][1]= u的儿子个数 也很好理解


代码:


#include<cstdio>
#include<cstring> 
#include<vector>
#include<algorithm>
using namespace std;
vector <int>T[155];
int dp[151][151];
int parent[151];
int n,p;
int find_min(int a,int b)
{
	return a<b?a:b;
}
void dfs(int u)
{
	dp[u][1]=T[u].size();
	if(T[u].empty())return ;
	
	int i,son,j,k,num=T[u].size();
	for ( i = 0; i < num; i++)
	{
		son=T[u][i];
		if (parent[son]==u)
		{
			dfs(son);
			for ( j = p; j >=1 ; j--)
			{
				for ( k = 0; k<=j-1; k++)
				{
					dp[u][j]=find_min(dp[u][j],dp[son][k]+dp[u][j-k]);
				}
			}
		}
	}
}

int main()
{
	int i,father,son,root,j,ans;
	while (scanf("%d%d",&n,&p)!=EOF)
	{
		ans=9999999;
		memset(parent,0,sizeof(parent));
		for ( i = 1; i <=n ; i++)
		{
			dp[i][0]=0;
			for ( j = 1; j <=p ; j++)
			{
				dp[i][j]=9999999;
			}
		}
		for ( i = 1; i <=n ; i++)
		{
			T[i].clear();
		}
		for ( i = 1; i <=n-1 ; i++)
		{
			scanf("%d%d",&father,&son);
			T[father].push_back(son);
			T[son].push_back(father);
			parent[son]=father;
		}
		for ( i = 1; i <=n ; i++)
		{
			if (parent[i]==0)
			{
				root=i;
				break;
			}
		}
		dfs(root);
		for ( i = 1; i <n ; i++)
		{
			if (dp[i][p]<ans)
			{
				ans=dp[i][p];
			}
		}
		if (n==1)printf("0\n");
		else printf("%d\n",ans-2*(p-1));
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值