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\n",s/2);//因为上边双向同时标记所以s算重复了  所以要除以2
	}
	return 0;
}
时间: 2024-08-27 06:00:08

hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】的相关文章

hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6701    Accepted Submission(s): 3358 Problem Description As we all know, machine scheduling is a very classical problem in comput

HDOJ 1054 Strategic Game

二分图匹配: 最大匹配数=最大独立集=最小点覆盖 最小路径覆盖=点数-最大匹配数 Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2147 Problem Description Bob enjoys playing computer ga

HDU1054 Strategic Game——匈牙利算法

Strategic Game 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 t

POJ 3041 Asteroids(二分图 &amp;&amp; 匈牙利算法 &amp;&amp; 最小点覆盖)

嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边,而我们要做的就是要找尽量少的点把所有的边覆盖,即为最小点覆盖问题,根据König定理:最小覆盖点数=最大匹配数,所以就可以用匈牙利算法求最大匹配了. AC代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream&

Asteroids POJ - 3041 匈牙利算法+最小点覆盖K&#246;nig定理

题意: 给出一个N*N的地图N   地图里面有K个障碍     你每次可以选择一条直线 消除这条直线上的所有障碍  (直线只能和列和行平行) 问最少要消除几次 题解: 如果(x,y)上有一个障碍 则把X加入点集 V1 .Y加入点集V2   并且X Y连一条边  这样构成一个新图 如果选择 V1中的点 X 那么就相当于消去 (X,y)中的所有Y    要找使最小点覆盖 那就是跑一遍 匈牙利就行了 详细证明见二分图最小点覆盖K?nig定理 其中 x y需要连单向边 不然会造成混乱 因为x=1 y=1

hdu 1054 Strategic Game (最小顶点覆盖+稀疏图)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4889    Accepted Submission(s): 2225 Problem Description Bob enjoys playing computer games, especially strategic games, but some

hdu 1054 Strategic Game 【匈牙利算法】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1054 题意:求无向图的最小顶点覆盖 = 最大匹配数 / 2; 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functiona

HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,并且该节点不放,所需的最少的点数. dp[i][1]表示i为根节点,并且该节点放,所需要的最少的点数. dp[i][0]=sum(dp[son[i][j]][1]) 该点不放,则它的儿子节点必须都放,只有这样之间的边才可以被覆盖. dp[i][1]=sum(min(dp[son[i][j]][0],dp[son[i][j]][1])) 该点放的话,则它的

HDU 1054 Strategic Game 最小点覆盖

 最小点覆盖概念:选取最小的点数覆盖二分图中的所有边. 最小点覆盖 = 最大匹配数. 证明:首先假设我们求的最大匹配数为m,那么最小点覆盖必然 >= m,因为仅仅是这m条边就至少需要m个点.然后假如我们已经求得最小覆盖点集,那么在点集中每个点必然有着这样的性质,在于它相连的边里面,一定有一条边的端点不在最小点集中,因为如果连一条这样的边都没有,那这个点完全没有在最小点集的必要,我们任意选取这样的一条边,一定可以形成一个匹配,匹配数与最小点集中的点的个数相等,但现在这仅仅是一个匹配,他必然小于最大