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

Hungarian algorithm is proposed by the Hungarian mathematician Edmonds in 1965, hence the name. Hungarian algorithm is thought sufficient proof of the theorem Hall-based, it is the most common of the Ministry of graph matching algorithm, the core of the algorithm is to find augmenting path, it is a demand by augmenting path bipartite graph maximum matching algorithm.

------- etc., see big head? So consider the following version:

 Note: The following switched http://blog.csdn.net/dark_scope/article/details/8880547

By generations of effort, you finally caught up with the tide of left men left woman, assuming you are a glorious new century matchmaker, there are N remaining men in your hands, M a woman left, everyone is likely to more than Interested in heterosexual ( Surprise-_- || not to consider specific sexual orientation), if a couple each have a crush, then you can put the pair together, to now let's ignore all out of unrequited love (good sad feeling almost cry), you have probably follows a diagram, each connection have expressed mutual goodwill.

 

 

The spirit of a life, build a seven wins Buddha principle of saving lives, the more you try to fix a couple as much as possible, working mode Hungary algorithm will teach you to do this:

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

A:  first try to find the sister No. 1 boys, girls first discover No. 1 and his name is connected also to spend no master, got it, even on a blue line

 

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

Two : Next to the No. 2 guy looking for my sister, Discovery spent the first 2 girls name and he connected without the Lord, got it

 

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

Three : Next is the No. 3 boys, we regret No. 1 girl already has a primary, and how to do it?

We tried to No. 1 before the boys and girls matches (that is, No. 1 boys) additionally assigned a sister.

 

(Yellow indicates that the edge is temporarily removed)

 

No. 1 boys and girls is connected to the second number 2 girls, 2 girls but also the Lord, how to do it? We then try to wife No. 2 girls ( Get angryGet angry) to re-find a sister (Note that this step is the same as above, which is a recursive process)

 

 

At this time, the boys found No. 2 No. 3'll find the girls, then the problem is solved before, back to back

 

No. 2 No. 3 boys can find sister ~ ~ ~ No. 1 boys can find the sister No. 2 ~ 3 boys can find a number 1 sister

The third step so the final result is:

 

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

: 接下来是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);
			the while (m -) {
				Scanf ( "% D", & B); 
				Y [A +. 1] [B +. 1] =. 1; 
			} 
		} 
		K = 0; 
		for (I =. 1; I <= n-; I ++) { 
			Memset (Book, 0, the sizeof (Book)); 
			IF (Yue (I) ==. 1) 
				K ++; 
		} 
		the printf ( "% D \ n-", NK / 2); // since this question (1, 2) with (2,1) is the same, it is divided by k to obtain 2; 
	} 
	return 0; 
}

  

Guess you like

Origin www.cnblogs.com/clb123/p/11778709.html
Recommended