Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5532 | Accepted: 2685 |
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 (1 ≤ G, B ≤ 200) and M (0 ≤ 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 (1 ≤ X≤ G,1 ≤ Y ≤ B), which indicates that girl X and boy Y know each other.
The girls are numbered from 1 to G and the boys are numbered from 1 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 1) followed by a integer which is the maximum number of kids the teacher can pick.
Sample Input
2 3 3 1 1 1 2 2 3 2 3 5 1 1 1 2 2 1 2 2 2 3 0 0 0
Sample Output
Case 1: 3 Case 2: 4
最大团转化成其的补图的最大独立集,其补图必定是二分图,因为原图所有男生都相互认识,所有女生都相互认识,其补图必然同类之间都不认识。
这个问题可以看我的另一篇博客:二分图的最大独立集
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 210
using namespace std;
int adj[Maxn][Maxn];
int match[Maxn];
int vis[Maxn];
int x,y;
int deep;
bool dfs(int u){
for(int v=1;v<=y;v++){
if(adj[u][v]&&vis[v]!=deep){
vis[v]=deep;
if(match[v]==-1||dfs(match[v])){
match[v]=u;
return true;
}
}
}
return false;
}
int hungary(){
memset(match,-1,sizeof match);
memset(vis,-1,sizeof vis);
int ans=0;
for(int i=1;i<=x;i++){
deep=i;
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
int m,a,b,cas=1;
while(cin>>x>>y>>m,x){
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
adj[i][j]=1;
for(int i=0;i<m;i++){
cin>>a>>b;
adj[a][b]=0;
}
printf("Case %d: %d\n",cas++,x+y-hungary());
}
return 0;
}