Title Description:
RPG girls is going to the amusement park with you today. Finally, you can take the roller coaster of your dreams. However, there are only two seats in each row of the roller coaster, and there is an unwritten rule that every girl must find a boy to be a partner and sit with her. However, every girl has her own ideas. For example, Rabbit is only willing to be a partner with XHD or PQK, Grass is only willing to be a partner with Linde or LL, and PrincessSnow is willing to be a partner with water Ranger or pseudo cool. Considering the funding problem, boss Liu decided to only let the people who found the partner take the roller coaster. The others, hehe, just stand and watch. Smart Acmer, can you help me figure out how many pairs of combinations can get on the roller coaster at most?
Input:
The first row of input data is three integers K, m and N, which respectively represent the number of possible combinations, the number of girls and the number of boys. 0<K<=1000
1 < = n and m < = 500. In the next K lines, there are two numbers in each line, indicating that female Ai is willing to work as a partner with male Bj. The last 0 ends the input.
Output:
For each group of data, an integer is output, indicating the maximum number of combinations that can ride the roller coaster
Sample input:
6 3 3
1 1
1 2
1 3
2 1
2 3
3 1
0
Sample output:
3
Hungarian algorithm can be regarded as marriage matching, and find the algorithm of maximum matching from bipartite graph.
#include<stdio.h> #include<iostream> #include<algorithm> #include<stdlib.h> #include<cstdlib> #include<malloc.h> #include<math.h> #include<string> #include<queue> #include<stack> #include<cstring> #include<vector> #include<map> #define inf 0x3f3f3f3f #define MAXN 510 #define eps 1e-8 #define LL long long #define MAX 1000000007 #define pi 3.1415926 #define e 2.71828182845 using namespace std; int dirx[8]={-1,0,1,0,1,-1,1,-1}; int diry[8]={0,1,0,-1,-1,1,1,-1}; int k,m,n; int link[MAXN][MAXN];///Whether there is edge connection between men and women int used[MAXN];///Tag array int girl[MAXN];///Which boy is this girl connected to int found(int u) { for(int i=1;i<=n;i++)///Traverse every girl { if(link[u][i]==1&&used[i]==0)///If two points are connected and not marked { used[i]=1; if(girl[i]==0||found(girl[i]))///There is no boy connected with this girl or you can free this boy to connect with other boys { girl[i]=u; return 1; } } } return 0; } int main() { while(scanf("%d",&k)&&k) { int x,y; scanf("%d%d",&m,&n); memset(link,0,sizeof(link)); memset(girl,0,sizeof(girl)); for(int i=0;i<k;i++) { scanf("%d%d",&x,&y); link[x][y]=1; } int ans=0; for(int i=1;i<=m;i++)///Traverse every boy { memset(used,0,sizeof(used)); if(found(i)) ans++; } printf("%d\n",ans); } return 0; }