Hang electric brush oj title (2063)

Coaster (maximum seek bipartite graph matching)

Subject description:

RPG girls today and we went to the playground to play, finally able to get on a roller coaster dream. However, the roller coaster of each row of only two seats, but also the unwritten rule is that every girl must find a partner and her boys to do the same sitting. However, each girl has their own ideas, for example the, Rabbit and XHD only willing to do or PQK partner, Grass and linle only willing to do or LL partner, PrincessSnow willing to do the waters of the prodigal son or pseudo Queer partner. Taking into account the financial implications, boss Liu decided to just let people find a partner to sit roller coaster, other people, hey, I stood below watching. Smart Acmer, you can help calculate a maximum number of combinations you can get on a roller coaster?

Input

The first line of input data is three integer K, M, N, respectively, the number of the possible number of combinations, the number of girls, boys. 0 <K <= 1000
. 1 <= N, and M <= 500. The next K rows, each row has two numbers, respectively, male and female Ai Bj willingness to do partner. 0 end of the last input.

Output

For each test, output an integer representing the number of combinations can sit up roller coaster.

Sample Input

6 3 3 
1 1 
1 2 
1 3 
2 1 
2 3 
3 1 
0

Sample Output

3

analysis:

Seeking bipartite graph maximum matching, the core is to find the maximum augmenting path by enumerating, make up a pair another pair.

By the answer:

#include<stdio.h>
#include<string.h>
int k,m,n;               //分别表示可能的组合数目,女生的人数,男生的人数
int map[510][510];       // 表示图的数组 
int visit[510];        //标记是否访问过 
int mat[510];           //标记是否有匹配对象 
int find(int x){       //配对函数    
    	for(int i=1;i<=m;i++){        //有m个小姑娘需要询问 
    		if(visit[i]==0&&map[i][x]){    //未访问过,并且能同意 
    			visit[i]=1;             //设置为已访问过 
    			if(mat[i]==-1||find(mat[i])){    //未配对且其能配对的男生可与其他女生配对 
    				mat[i]=x;        //配对 
    				return 1;
				}
			}
		}
	return 0;
}
int main(){
    
    while(scanf("%d",&k)!=EOF){
    	if(k==0)break;              //0表示输入结束 
    	scanf("%d%d",&m,&n);
    	memset(map,0,sizeof(map));   //初始化:将指针变量 map所指向的前 n 字节的内存单元用一个“整数”0替换 
    	memset(mat,-1,sizeof(mat));
    	for(int i=0;i<k;i++){      
    		int a,b;
    		scanf("%d%d",&a,&b);  //分别表示女生Ai愿意和男生Bj做partner 
    		map[a][b]=1;
		}
		int output=0;
		for(int i=1;i<=n;i++){      //有n个男生 
			memset(visit,0,sizeof(visit));
			if(find(i))
			    output++;           //计算对数 
		}
		printf("%d\n",output);	
	}
	return 0;
}

 

 

 

Published 76 original articles · won praise 3 · Views 1861

Guess you like

Origin blog.csdn.net/ZhangShaoYan111/article/details/104334399