标签:
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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
#include <cstring>
#include <string>
#define MAXN 210
#define inf 0xffffffff
using namespace std;
int map[MAXN][MAXN],vis[MAXN],pre[MAXN];
int g,b;
int find(int cur)
{
for(int i=1; i<=b; ++i)
{
if(!vis[i]&&!map[cur][i]) //map求反是关键!因为是要求补图的最大独立集
{
vis[i] = true;
if(pre[i] == 0 || find(pre[i]))
{
pre[i] = cur;
return 1;
}
}
}
return 0;
}
int main()
{
int m,x,y,cnt=1;
while(~scanf("%d%d%d",&g,&b,&m))
{
memset(map,0,sizeof(map));
memset(pre,0,sizeof(pre));
if(g==0&&b==0&&m==0)
break;
for(int i=1; i<=m; ++i)
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
int ans=g+b;
for(int i=1; i<=g; ++i)
{
memset(vis,0,sizeof(vis));
if(find(i))
ans--;
}
printf("Case %d: %d\n",cnt++,ans);
}
return 0;
}
poj3690——Kindergarten(最大独立点集,匈牙利算法)
标签:
原文地址:http://blog.csdn.net/blue_skyrim/article/details/51329232