Description
In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know each other. In addition to that, some girls and boys know each other. Now the teachers want to pick some kids to play a game, which need that all players know each other. You are to help to find maximum number of kids the teacher can pick.
Input
The input consists of multiple test cases. Each test case starts with a line containing three integers
G, B ( ≤ G, B ≤ ) and M ( ≤ M ≤ G × B), which is the number of girls, the number of boys and
the number of pairs of girl and boy who know each other, respectively.
Each of the following M lines contains two integers X and Y ( ≤ X≤ G, ≤ Y ≤ B), which indicates that girl X and boy Y know each other.
The girls are numbered from to G and the boys are numbered from to B. The last test case is followed by a line containing three zeros.
Output
For each test case, print a line containing the test case number( beginning with ) followed by a integer which is the maximum number of kids the teacher can pick.
Sample Input
Sample Output
Case :
Case :
Source
求的是最大独立集,但是这里要用mp[][]==0来匹配,真是太妙了,用到了逆思维,说明自己想问题还不够深刻啊,还需要继续努力!!!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 206
int G,B,M;
int mp[N][N];
int match[N];
int vis[N];
bool dfs(int x){
for(int i=;i<=B;i++){
if(!vis[i] && !mp[x][i]){//注意这里要取mp[x][i]==0!!!
vis[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return true;
}
}
}
return false;
}
int solve(){
int ans=;
memset(match,-,sizeof(match)); for(int i=;i<=G;i++){
memset(vis,,sizeof(vis));
if(dfs(i)){
ans++;
}
}
return ans;
}
int main()
{
int ac=;
while(scanf("%d%d%d",&G,&B,&M)==){
if(G== && B== && M==)
break;
printf("Case %d: ",++ac);
memset(mp,,sizeof(mp));
for(int i=;i<M;i++){
int x,y;
scanf("%d%d",&x,&y);
mp[x][y]=;
}
int ans=solve(); printf("%d\n",G+B-ans); }
return ;
}