hdu1069 Girls and Boys

the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set. 

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description: 

the number of students 
the description of each student, in the following format 
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ... 
or 
student_identifier:(0) 

The student_identifier is an integer number between 0 and n-1, for n subjects. 
For each given data set, the program should write to standard output a line containing the result. 

Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Output

5
2

Sample Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0

Sample Output

5
2

匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法。

-------等等,看得头大?那么请看下面的版本:

 注:以下转自 http://blog.csdn.net/dark_scope/article/details/8880547

通过数代人的努力,你终于赶上了剩男剩女的大潮,假设你是一位光荣的新世纪媒人,在你的手上有N个剩男,M个剩女,每个人都可能对多名异性有好感(惊讶-_-||暂时不考虑特殊的性取向),如果一对男女互有好感,那么你就可以把这一对撮合在一起,现在让我们无视掉所有的单相思(好忧伤的感觉快哭了),你拥有的大概就是下面这样一张关系图,每一条连线都表示互有好感。


 

本着救人一命,胜造七级浮屠的原则,你想要尽可能地撮合更多的情侣,匈牙利算法的工作模式会教你这样做:

===============================================================================

一: 先试着给1号男生找妹子,发现第一个和他相连的1号女生还名花无主,got it,连上一条蓝线

 

===============================================================================

:接着给2号男生找妹子,发现第一个和他相连的2号女生名花无主,got it

 

===============================================================================

:接下来是3号男生,很遗憾1号女生已经有主了,怎么办呢?

我们试着给之前1号女生匹配的男生(也就是1号男生)另外分配一个妹子。

 

(黄色表示这条边被临时拆掉)

 

与1号男生相连的第二个女生是2号女生,但是2号女生也有主了,怎么办呢?我们再试着给2号女生的原配(发火发火)重新找个妹子(注意这个步骤和上面是一样的,这是一个递归的过程)


 

此时发现2号男生还能找到3号女生,那么之前的问题迎刃而解了,回溯回去

 

2号男生可以找3号妹子~~~                  1号男生可以找2号妹子了~~~                3号男生可以找1号妹子

所以第三步最后的结果就是:

 

===============================================================================

: 接下来是4号男生,很遗憾,按照第三步的节奏我们没法给4号男生腾出来一个妹子,我们实在是无能为力了……香吉士同学走好。

 

===============================================================================

这就是匈牙利算法的流程,其中找妹子是个递归的过程,最最关键的字就是“腾”字

其原则大概是:有机会上,没机会创造机会也要上.

这是匈牙利算法的理解;

然后再看这道题;

 

 

#include <stdio.h>
#include <string.h>
int book[10010];
int match[10010];
int y[1010][1010];
int n;

int yue(int u) //匈牙利算法
{
	int v,i,j;
	for(i=1; i <= n; i++){
		if(book[i] == 0 && y[u][i] == 1){
			book[i]=1;
			if(match[i]==0||yue(match[i])){
				match[i]=u;
				return 1;
			}
		}
	}
	return 0;
}

int main()
{
	int i,j,k,a,b,m;
	while(scanf("%d",&n)!=EOF)
	{
		memset(y,0,sizeof(y));
		memset(match,0,sizeof(match));
		for(i=0;i<n;i++){
			scanf("%d: (%d)",&a,&m);
			while(m--){
				scanf("%d",&b);
				y[a+1][b+1]=1;
			}
		}
		k=0;
		for(i=1;i<=n;i++){
			memset(book,0,sizeof(book));
			if(yue(i)==1)
				k++;
		}
		printf("%d\n",n-k/2);  //因为这道题(1,2)跟(2,1)是一样的,所以k得除以2;
	}
	return 0;
}

  

posted @ 2019-11-01 18:44  陈墨cacm  阅读(2663)  评论(0编辑  收藏  举报