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 0Sample Output
Case 1: 3 Case 2: 4
题意:有g个女孩和b个男孩,输入m对数,每对数a,b,表示编号a女孩认识编号b女孩,选出尽可能多的相互认识的人。
先区分一些概念:
最大匹配=最小顶点覆盖
最大团 :在v中取k个顶点,两点间相互连接
最大独立集:在v中取k个顶点,两点间不相互连接
最大团数量:在补图中,最大独立集数。
最大独立集 = n-最小顶点覆盖=n-最大匹配
思路:很明显是一道二分匹配问题,但是平时我们用的匈牙利算法都是求最小顶点覆盖,这道题反而求最大顶点覆盖?反过来思考,我们可以求这道题的补图(也就是不相互认识的人)的最小顶点覆盖,用n减去最小顶点覆盖就是最大独立集,也就是最多尽可能认识的人
今天补题补完还有时间的话,就回来用邻接表实现一遍
//邻接矩阵实现
#include<stdio.h>
#include<string.h>
const int maxn = 220;
int vis[maxn][maxn],f[maxn],book[maxn];
int g,b,m;
int dfs(int u)
{
int i;
for(i = 1; i <= b; i ++)
{
if(!book[i]&&vis[u][i])
{
book[i] = 1;
if(f[i]==-1||dfs(f[i]))
{
f[i] = u;//记录i的增广路径
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,x,y,sum,t=0;
while(scanf("%d%d%d",&g,&b,&m),g!=0||b!=0||m!=0)
{
for(i = 0; i <= g; i ++)
for(j = 0; j <= b; j ++)
vis[i][j] = 1; //补图初始化
for(i = 0; i < m; i ++)
{
scanf("%d%d",&x,&y);
vis[x][y] = 0;//配对边初始化0
}
memset(f,-1,sizeof(f));
sum = 0;
for(i = 1; i<= g; i ++)
{
memset(book,0,sizeof(book));
if(dfs(i))//求最大匹配
sum ++;
}
printf("Case %d: %d\n",++t,g+b-sum);
}
return 0;
}