hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】

Strategic Game

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


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: 





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
#include<stdio.h>
#include<string.h>
#define MAX 1550
int map[MAX][MAX],vis[MAX],point[MAX];
int t;
int find(int x)
{
	int i;
	for(i=0;i<t;i++)
	{
		if(map[x][i]&&!vis[i])
		{
			vis[i]=1;
			if(point[i]==0||find(point[i]))
			{
				point[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int i,j,s,n,m;
	int a;
	while(scanf("%d",&t)!=EOF)
	{
		memset(map,0,sizeof(map));
		memset(point,0,sizeof(point));
		for(i=0;i<t;i++)
		{
			scanf("%d:(%d)",&n,&m);
			for(j=0;j<m;j++)
			{
				scanf("%d",&a);
				map[n][a]=map[a][n]=1;//此处要将map[n][a]和map[a][n]同时标记 
			}                       
		}
		s=0;
		for(i=0;i<t;i++)
		{
			memset(vis,0,sizeof(vis));
			if(find(i))
			s++;
		}
		printf("%d
",s/2);//因为上边双向同时标记所以s算重复了  所以要除以2 
	}
	return 0;
}
原文地址:https://www.cnblogs.com/tonghao/p/4668577.html