设为首页 加入收藏

TOP

hdu 1054 Strategic Game(tree dp)
2014-11-23 21:34:22 来源: 作者: 【 】 浏览:6
Tags:hdu 1054 Strategic Game tree

Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3806 Accepted Submission(s): 1672

Problem 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 medieva l 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.

The input file 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_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:

\

the solution is one soldier ( at the node 1).

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 table:


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


Source
Southeastern Europe 2000

#include
#include
#include
using namespace std;
#define N 2010
int Min(int i,int j){return il;
	while(l){
		dfs(l);
		//放在x上
		x->f[1]+=Min(l->f[0],l->f[1]);
		//x不放
		x->f[0]+=l->f[1];
		l=l->r;
	}
}
int main(){
	int i,j,k,x,y,n,m;
	while(~scanf("%d",&n)){
		m=0;
		memset(r,-1,sizeof(r));
		while(n--){
			scanf("%d:(%d)",&i,&k);
			if(r[i]==-1){r[i]=m;t[m++].init();}
			x=r[i];
			while(k--){//i->l=j
				scanf("%d",&j);
				if(r[j]==-1){r[j]=m;t[m++].init();}
				y=r[j];
				t[y].f[1]=1;
				t[y].r=t[x].l;
				t[x].l=&t[y];
			}
		}
		for(i=0;;i++)
			if(t[r[i]].f[1]==0){
				rt=&t[r[i]];
				rt->f[1]=1;
				break;
			}
		dfs(rt);
		printf("%d\n",Min(rt->f[0],rt->f[1]));
	}
return 0;
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 2871 Memory Control 伸展树.. 下一篇HDU 1556 Color the ball

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·怎样用 Python 写一 (2025-12-27 02:49:19)
·如何学习python数据 (2025-12-27 02:49:16)
·想要自学数据分析, (2025-12-27 02:49:14)
·Java 集合框架 - 菜 (2025-12-27 02:19:36)
·Java集合框架最全详 (2025-12-27 02:19:33)