POJ题目1947 Rebuilding Roads(树形dp)

Rebuilding Roads

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9957   Accepted: 4537

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

USACO 2002 February

题目大意:问一个数删掉最少条边变成一个仅仅有n个结点的子树

ac代码

#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b?

b:a)
#define INF 0xfffffff
int dp[220][220];
int pre[220],head[220],vis[220],dig[220];
int n,p,cnt;
struct s
{
	int u,v,w,next;
}edge[220*2];
void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void tree_dp(int u)
{
	int i,j,k;
	for(i=0;i<=p;i++)
	{
		dp[u][i]=INF;
	}
	dp[u][1]=0;
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		tree_dp(v);
		for(k=p;k>=1;k--)
		{
			dp[u][k]=dp[u][k]+1;
			for(j=1;j<k;j++)
			{

				dp[u][k]=min(dp[u][k],dp[u][j]+dp[v][k-j]);
			}
		}
	}
}
int DP(int u)
{
	tree_dp(u);
	int ans=dp[u][p];
	int i;
	for(i=1;i<=n;i++)
	{
		ans=min(ans,dp[i][p]+1);
	//	printf("%d\n",dp[i][1]);
	}
	return ans;
}
int main()
{
	//int n,p;
	while(scanf("%d%d",&n,&p)!=EOF)
	{
		int i;
		memset(dig,0,sizeof(dig));
		memset(head,-1,sizeof(head));
		cnt=0;
		for(i=0;i<n-1;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			add(a,b);
			dig[b]++;
		}
		int root;
		for(i=1;i<=n;i++)
		{
			if(dig[i]==0)
				root=i;
		}
		printf("%d\n",DP(root));
	}
}
时间: 2024-08-01 10:09:00

POJ题目1947 Rebuilding Roads(树形dp)的相关文章

POJ 1947 Rebuilding Roads (树形dp 经典题)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9499   Accepted: 4317 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

[POJ 1947]Rebuilding Roads (树形dp)

题目链接:http://poj.org/problem?id=1947 题目大意:给你一棵树,树上N个节点.问最少拆掉多少条边使得存在一个联通块,有P个节点. 树形dp,设计状态:dp[u][i]代表以u为根节点的剩下i个节点最少需要拆掉多少条边. 状态转移:dp[u][i+j] = min(dp[u][i+j],dp[u][i]+dp[v][j]-1); 其中v是u的儿子节点. 相当于在树上跑01背包,即每个儿子节点去掉剩下j个的但是要连上u-v边,或者不去掉剩下j个的. 代码: 1 impo

[poj 1947] Rebuilding Roads 树形DP

Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 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 co

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

题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去的边数. 考虑两种情况,去掉孩子节点v与去不掉. (1)去掉孩子节点:dp[u][j] = dp[u][j] + 1 (2)不去掉孩子节点:dp[u][j] = min(dp[u][j - k] + dp[v][k]) 综上就是dp[u][j] = min(dp[u][j] + 1, min(dp[

POJ 3345——Bribing FIPA(树形DP)

题目分析: 现在有n个村子,你想要用收买m个国家为你投票,其中收买第i个国家的代价是val[i].但是有些国家存在从属关系,如果B从属于A国,则收买了A也意味着买通了B,而且这些关系是传递的.问你最小要付出的代价是多少? 这题的难点在于怎么建图,弱菜不会,只能膜拜大神的代码,然后自己捉摸着敲,dfs部分就和一般的树形DP+背包差不多,只是状态的初始化有些变化 建图需要加个超级根,连接大国(小国和大国是从属关系) dp [ i ] [ j ]表示以 i 为根取得 j 票所需要的最小费用 大牛用了m

poj 4045 Power Station(初涉树形dp)

http://poj.org/problem?id=4045 大致题意:有n个村庄,求将发电站建在哪一个村庄使得花费最少.这是一个无向无环图.简化一下就是求一个节点使它到其他所有节点的距离和最小. 起初一直在向最短路上靠,但因为节点和边数太大,必定TLE.然后无比强大的啸神随便写了两个dfs就过掉了,简直膜拜.赛后搜了搜题解,发现这是道树形dp.sad,真的要好好刷dp了. 大体思路是将这个无向无环图看做一个树,我们就在这个树上进行动态规划.首先先随便拿一个节点看做根节点(假设节点1),计算出它

树的点分治 (poj 1741, 1655(树形dp))

poj 1655:http://poj.org/problem?id=1655 题意: 给无根树,  找出以一节点为根,  使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子树的节点数. 再dfs  找出以某节点为根的最大子树,节点最少. 复杂度(n) /***Good Luck***/ #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cs

poj 2342 Anniversary party,树形DP easy

poj 2342 Anniversary party 没有上司的晚会 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数最大.但是,没有职员愿和直接上司一起与会. 程序名:party 输入格式: 第一行一个整数N.(1<=N<=6000) 接下来N行,第i+1行表示i号职员的快乐指数Ri.(-128<=Ri<=127) 接下来N-1行,每行输入

poj 1947 Rebuilding Roads 【树形DP】 【求至少删去树中 多少条边 使得树中节点数为P】

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10066   Accepted: 4595 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. Th