天天看点

树形Dp——POJ 1463 Strategic game

Strategic game

Time Limit: 2000MS Memory Limit: 10000K

Description

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:

树形Dp——POJ 1463 Strategic game

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

译: [Description]

Bob 特别喜欢战略游戏,但有时他不能尽快找到最优解, 所以他就很伤心。现在他又有一个

问题,他必须保卫一个中世纪的城市,这个城市的道路形成了一棵树。他需要在树的节点上

放最少的士兵来观察所有的边。你能帮助他么?

例如右图就只需要一个士兵放在 1 号节点。

树形Dp——POJ 1463 Strategic game

[Input]

多组数据直到 EOF

对于每组数据第一行 N 表示点的个数。接下来 N 行每行如下

x:(k) a1 a2 … ak

(x 为点的编号,k 为与其相连的子节点个数,a1, a2, …, ak 分别为子节点的编号)

[Output]

对于每组数据输出一行一个数,即最少士兵数

[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

[Hint]

0 < N <= 1500, 0 <= x < N

#include<stdio.h>
int n,idx;
int head[1501];
int next[3001];
int to[3001];
int f[1501];
int g[1501];
int val[1501];
long long min(long long a,long long b)
{
	if(a<b)
		return a;
	return b;
}
void play(int from,int p)
{
	f[p]=1;
	g[p]=0;
	for(int i=head[p];i;i=next[i])
		if(to[i]!=from)
		{	
			play(p,to[i]);
			f[p]+=min(f[to[i]],g[to[i]]);
                        /*‘父亲结点放置了,儿子结点可以放置也可以不放置’*/
                        g[p]+=f[to[i]];
                       /*‘父亲结点没有放置,儿子结点必须放置’*/
                 }
}
int main()
{
	while(scanf("%d",&n)==1)
	{
		idx=0;
		for(int i=1;i<=n;i++)
			head[i]=0;
		for(int i=1;i<=n;i++)
		{
			int a,m;
			char c,b;
			scanf("%d",&a);
			if(!a)
				a=n;
			scanf("%c%c",&c,&b);
			scanf("%d",&m);
			scanf("%c",&c);
			for(int j=1;j<=m;j++)
			{
				int b;
				scanf("%d",&b);
				if(!b)
					b=n;
				next[++idx]=head[a];
				head[a]=idx;
				to[idx]=b;
				next[++idx]=head[b];
				head[b]=idx;
				to[idx]=a;
			}
		}
		play(0,1);
		printf("%d\n",min(f[1],g[1]));
	}
}