POJ3692:Kindergarten(最大团)

来源:互联网 发布:三菱plc3u编程手册 编辑:程序博客网 时间:2024/03/29 12:39

Kindergarten
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7223 Accepted: 3554

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
题意:G个女互相认识,B个男互相认识,有M对男女关系互相认识,问最多找到几个人两两都认识。

思路:显然是找最大团,但直接建边太多了,又最大团=补图的最大独立集,而它的补图又是个二分图,最大独立集=最大匹配数-点数,跑匈牙利即可。

# include <iostream># include <cstdio># include <cstring>using namespace std;int n, m, g[203][203], l[203], vis[203];int dfs(int u){    for(int i=1; i<=m; ++i)    {        if(!g[u][i] && !vis[i])        {            vis[i] = 1;            if(l[i] == -1 || dfs(l[i]))            {                l[i] = u;                return 1;            }        }    }    return 0;}int main(){    int k, cas=1;    while(~scanf("%d%d%d",&n,&m,&k),n+m+k)    {        memset(g, 0, sizeof(g));        memset(l, -1, sizeof(l));        while(k--)        {            int x, y;            scanf("%d%d",&x,&y);            g[x][y] = 1;        }        int ans = 0;        for(int i=1; i<=n; ++i)        {            memset(vis, 0, sizeof(vis));            if(dfs(i)) ++ans;        }        printf("Case %d: %d\n",cas++,n+m-ans);    }    return 0;}



原创粉丝点击