树形dp总结+例题

树形DP的特殊性:没有环,dfs是不会重复,而且具有明显而又严格的层数关系。
判断数据结构是否是一棵树,然后是否符合动态规划的要求。

建树:通过数据量和题目要求,选择合适的树的存储方式。

如果节点数小于5000,那么我们可以用邻接矩阵存储,如果更大可以用邻接表来存储,注意边要开到2*n,因为是双向的。如果是二叉树或者是需要多叉转二叉,那么我们可以用两个一维数组brother[],child[]来存储。

例题:

1.Strategic game POJ - 1463 (树的最小点覆盖)
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

For example for the tree:

the solution is one soldier ( at the node 1).
Input
The input contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 … node_identifiernumber_of_roads
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
Output
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:
Sample Input
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)
Sample Output
1
2
题意:给出一个n个结点的树,要求选出其中的一些顶点,使得对于树中的每条边(u, v),u和v至少有一个被选中. 请给出选中顶点数最少的方案.

#include
#include
#include
#include
using namespace std;
int n,t,x,y;
int dp[1505][2];
int vis[1505];
vector vec[1505];
void dfs(int x){
	dp[x][0]=0;
	dp[x][1]=1;
	vis[x]=1;
	for(int i=0;i

2.Rebuilding Roads POJ - 1947
链接
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.]

题意:有n个点组成一个树,问至少删多少条边才能获得一棵有p个节点的子树。
dp[i][j]表示以i为根,生成j个节点需要删的边数。
初始化:dp[i][j]=所有儿子的边+父亲的边=vt[i].size()+1;
求min初始化为inf
注意要在转移方程中减去2,因为父亲与儿子之间的边不用断,但方程中断了两次

#include
#include
#include
#include
using namespace std;
const int inf=0x3f3f3f;
vector vt[160];
int n,p;
int dp[160][160];
bool f[160];
void dfs(int root){
	int len=vt[root].size();
	for(int i=0;i1;j--){
			for(int k=1;k>x>>y;
		vt[x].push_back(y);
		f[y]=1;
	}
	int root;
	for(int i=1;i<=n;i++){
		if(!f[i]){
			root=i;
			break;
		}
	}
//	cout<

3.poj 4045 Power Station (树的重心)
题意:n个城市节点构成的一棵树,节点i到节点j的电量损耗为 IIR*(i到j的路径所含边数),现在要在某个结点上修建一个供电站,使得这个结点到所有其它节点的总损耗量最小。

方法一:找到树的重心,找树的重心到各个店的距离之和。
方法二:两遍dfs1.以1为根节点找到所有点到1的距离和dp[1]和子节点的个数d[x];
2.用dp[1]更新其他点,dp[v]=dp[u]-d[v]+(n-d[v])
方法一:

#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn=50005;
int t,n,I,R,root;
vector G[maxn];
int dp[maxn],sum[maxn],node[maxn],h[maxn],vis[maxn];
int dfs(int u,int fa){
	int maxx=0;
	int cnt=1;
	for(int i=0;i>t;
	while(t--){
		scanf("%d%d%d",&n,&I,&R);
		for(int i=1;i<=n;i++){
			G[i].clear();
		}
		int x,y;
	//	memset(vis,0,sizeof(vis));
		memset(dp,0,sizeof(dp));
		for(int i=1;i

你可能感兴趣的:(树形dp,dp)