hdu1069 Girls and Boys

hdu1069 Girls and Boysthe second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is def

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个剩女,每个人都可能对多名异性有好感(惊讶-_-||暂时不考虑特殊的性取向),如果一对男女互有好感,那么你就可以把这一对撮合在一起,现在让我们无视掉所有的单相思(好忧伤的感觉快哭了),你拥有的大概就是下面这样一张关系图,每一条连线都表示互有好感。

hdu1069 Girls and Boys


 

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

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

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

hdu1069 Girls and Boys

 

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

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

hdu1069 Girls and Boys

 

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

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

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

 

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

hdu1069 Girls and Boys

 

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


hdu1069 Girls and Boys

 

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

 

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

hdu1069 Girls and Boyshdu1069 Girls and Boyshdu1069 Girls and Boys

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

hdu1069 Girls and Boys

 

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

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

 

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

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

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

这是匈牙利算法的理解;

然后再看这道题;

hdu1069 Girls and Boys

 

 

#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;
}

  

今天的文章hdu1069 Girls and Boys分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/48475.html

(0)
编程小号编程小号
上一篇 2023-09-03
下一篇 2023-09-04

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注