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
Source
题意就是让你找出最多的人,他们之间是相互认识的,条件告诉我们男孩和男孩是相互认识的,女孩也是,而且部分男孩女孩认识,那么我们就可以以男孩为一个集合,女孩为一个集合,然后二分匹配,如果某个男孩和某个女孩不认识,我们就连一条边,进行最大匹配。然后求最大独立集,由于建边的缘故,最大独立集里的点其实是相互认识的。
#include<stdio.h>
#include<string.h>
const int maxn=205;
bool each[maxn][maxn];
struct node
{
int to;
int next;
}edge[maxn*maxn];
int head[maxn];
int mark[maxn];
bool used[maxn];
int tot;
int N;
void addedge(int from,int to)
{
edge[tot].to=to;
edge[tot].next=head[from];
head[from]=tot++;
}
bool dfs(int x)
{
for(int i=head[x];i!=-1;i=edge[i].next)
{
if(!used[edge[i].to])
{
used[edge[i].to]=1;
if(mark[edge[i].to]==-1 || dfs(mark[edge[i].to]))
{
mark[edge[i].to]=x;
return true;
}
}
}
return false;
}
int hungary()
{
int ans=0;
memset(mark,-1,sizeof(mark));
for(int i=1;i<=N;i++)
{
memset(used,0,sizeof(used));
if(dfs(i))
ans++;
}
return ans;
}
int main()
{
int G,B,M,icase=1;
while(~scanf("%d%d%d",&G,&B,&M))
{
if(!G && !B && !M)
break;
memset(each,0,sizeof(each));//数组值为0代表相互不认识
memset(head,-1,sizeof(head));
tot=0;
N=G;
int x,y;
for(int i=1;i<=M;i++)
{
scanf("%d%d",&x,&y);
each[x][y]=1;//相互认识
}
for(int i=1;i<=G;i++)
for(int j=1;j<=B;j++)
{
if(!each[i][j])//相互不认识的连边
addedge(i,j);
}
printf("Case %d: %d\n",icase++,G+B-hungary());
}
return 0;
}
本文介绍了一种通过二分匹配算法解决幼儿园老师如何挑选互相认识的孩子们参与游戏的问题。利用男孩和女孩之间的认识关系,构建图模型并进行最大匹配,最终确定能够参与游戏的最大孩子数量。

595

被折叠的 条评论
为什么被折叠?



