POJ 1947 Rebuilding Roads

Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6309 Accepted: 2724

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


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 200

struct LEdge{
	int ed, next;
}edge[2 * MAXN];
int head[MAXN], nEdge;

void init(){
	nEdge = 0;
	memset(head, 0xff, sizeof(head));
}

void addEdge(int i, int j){
	edge[nEdge].ed = j;
	edge[nEdge].next = head[i];
	head[i] = nEdge++;
}

int imin(int a, int b){
	return a < b ? a : b;
}

int f[MAXN][MAXN];
int N, M;

void dfs(int pre, int cur){
	int i, j, k, p, t;
	f[cur][0] = 1;
	f[cur][1] = 0;
	for (i = head[cur]; i != -1; i = edge[i].next){
		p = edge[i].ed;
		if (p == pre) continue;
		dfs(cur, p);
		for (j = M; j > 0; j--){
			t = 0;
			for (k = 1; k < j; k++){
				if (f[cur][j - t] + f[p][t] > f[cur][j - k] + f[p][k])
					t = k;
			}
			
			f[cur][j] = f[cur][j - t] + f[p][t];
//			printf("f[%d] = %d, t = %d\n", j, f[cur][j], t);
		}
//			printf("f[%d] = %d, t = %d\n", j, f[cur][j], 0);
	}
}

int Isroot[MAXN];
int main(){
	int i, j, k;
	while(scanf("%d%d", &N, &M) != EOF){
		init();
		memset(Isroot, 1, sizeof(Isroot));
		memset(f, 0x0f, sizeof(f));
		for (k = 1; k < N; k++){
			scanf("%d%d", &i, &j);
			addEdge(i, j);
			Isroot[j] = 0;
		}
		for (i = 1; i <= N; i++) if (Isroot[i]) break;
		dfs(0, i);
		f[i][0] = 0;
		k = f[i][M];
		for (i = 1; i <= N; i++){
			k = k > f[i][M] + 1 ? f[i][M] + 1 : k;
		}
		printf("%d\n", k);
	}
	return 0;
}

/*
最少地删去树中边,使分离出M个点
题意理解一:M个点可以为森林
f[i][j]表示以i为根的子树,分离出去j个点,最少删边数
题意理解二:M个点必须为一个棵树
f[i][j]表示以i为根的子树,保留j个点,最少删边数
初值纠结了,根源还是理解不深...
1.背包的含义
容易想到f[i][j] = min(f[p][k] + f[i][j - k])
相当于有容量为j的背包,在每个孩子结点中要选一个体积为k的物品放入,
分组背包,注意每个孩子结点中必须选且仅选一个物品放入
初值呢,
f[i][0]=0,表示不选就不用删边
f[i][1]=count(child[i])选一个,就只能是根,删去其他孩子结点
然后把j倒序地更新一遍,物品写在内层,就行了么?错!
注意啊,f[i][j]实际上还隐含着2维
f[i][p][k][j]表示以i为根的子树,在前p组中,第p组前k个物品中,填充容量j的最小花费
于是f[i][1]=count(child[i])表达的实际上是前所有组中,填充容量1的最小花费
这可不是初值!
好吧,我这里把孩子连向根的边算作子树的一部分,而不是根的。
于是f[i][1]=0,表示以i为根的子树,没开始放物品时,只有秃秃的根一个点,也就不存在什么边,所以也不需要删边。要说存在一条边,那就是这个根和他父亲结点的连边了。
所以f[i][0]=1,表示都也不取的话,就把与父亲相连的边也删掉
于是我写出下面这段转移
for j = M to 1
    t = 0;
    for k = 0 to j
	    if (f[i][j - t] + f[p][t] > f[i][j - k] + f[p][k]) t = k;
	f[i][j] = f[i][j - t] + f[p][t]
t的设置,使得每组必须选一个物品k
但上面的代码还是WA
2.不和谐的转移
k=j,在背包中看起来无伤大雅,但在树中
f[i][j-k] + f[p][k],表示原树中什么都不取,j个点全让子树p来取
这与f[i][j]的定义相矛盾,f[i][j]中只要j>0,那根是一定要取的
所以,k < j
这样下来终于AC了~
最后注意下分离的树中不一定包含总根,也可以是棵子树,需要遍历下所有子树
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值