Hdu 1054 Strategic Game

Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3714    Accepted Submission(s): 1630


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 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.

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:

Hdu 1054 Strategic Game_第1张图片

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
 
一城堡的所有的道路形成一个n个节点的树,如果在一个节点上放上一个士兵,那么和这个节点相连的边就会被看守住,问把所有边看守住最少需要放多少士兵。

树形DP

d[i][0]代表i结点不放士兵的最小取值
d[i][1]代表i结点放士兵的最小取值

#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

#define Min(a,b) (a<b?a:b)

int N;
bool vis[1600];
vector<int> son[1600];
int d[1600][2];

void DFS(int x)
{
	int v;
	d[x][0]=0;
	d[x][1]=1;
	for(int i=0;i<son[x].size();i++)
	{
		v=son[x][i];
		DFS(v);
		d[x][0]+=d[v][1];
		d[x][1]+=Min(d[v][0],d[v][1]);
	}
}

int main()
{
	int u,v,cnt,i;

	while(scanf("%d",&N)==1)
	{
		memset(vis,0,sizeof(vis));
		for(i=1;i<=N;i++)
		{
			scanf("%d",&u);
			scanf("%*c%*c%d%*c",&cnt);
			son[u].clear();
			while(cnt--)
			{
				scanf("%d",&v);
				son[u].push_back(v);
				vis[v]=1;
			}
		}
		for(i=0;i<N;i++)
			if(!vis[i])
			{
				DFS(i);
				break;
			}
		
		printf("%d\n",Min(d[i][0],d[i][1]));

/*		for(i=0;i<N;i++)
			printf("%d %d %d\n",i,d[i][0],d[i][1]);
*/
	}

	return 0;
}


你可能感兴趣的:(Hdu 1054 Strategic Game)