POJ 3692

来源:互联网 发布:迈科技 知乎 编辑:程序博客网 时间:2024/06/07 03:06
Kindergarten
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 4547 Accepted: 2228

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
GB (1 ≤ GB ≤ 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 31 11 22 32 3 51 11 22 12 22 30 0 0

Sample Output

Case 1: 3Case 2: 4

Source

2008 Asia Hefei Regional Contest Online by USTC

所有的男生相互认识,所有的女生相互认识,一些男生和一些女生相互认识。现在老师要选出一些人,要求这些人相互认识,现在问你最多能带出多少人。

本题是一个求解最大团的问题,最大团的问题可以转化为求补图上最大独立点集的问题。考虑到问题的特殊性,所有的男生相互认识,所有的女生相互认识,那么补图一定是一个二分图,问题就成了求解二分图最大独立点集。

#include<cstdio>#include<cstring>#define maxn 509using namespace std;int n,m;int a[maxn][maxn],cx[maxn],cy[maxn],vis[maxn];int path(int u){    int v;    for(int v=1;v<=m;v++)    {        if(a[u][v]&&!vis[v])        {            vis[v]=1;            if(cy[v]==-1||path(cy[v]))            {                cy[v]=u;                cx[u]=v;                return 1;            }        }    }    return 0;}int match(){    int res=0;    memset(cx,-1,sizeof(cx));    memset(cy,-1,sizeof(cy));    for(int i=1;i<=n;i++)    {        if(cx[i]==-1)        {            memset(vis,0,sizeof(vis));            res+=path(i);        }    }    return res;}int main(){    int num,cot=0;    while(scanf("%d%d%d",&n,&m,&num),n+m+num)    {        memset(a,0,sizeof(a));cot++;        for(int i=0;i<num;i++)        {            int x,y;            scanf("%d%d",&x,&y);            a[x][y]=1;        }        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)            a[i][j]=a[i][j]^1;        printf("Case %d: %d\n",cot,m+n-match());    }}


原创粉丝点击