二分图最大团-poj3692

来源:互联网 发布:ug软件好学吗 编辑:程序博客网 时间:2024/04/28 04:49
Language:
Kindergarten
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 4518 Accepted: 2215

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

Sample Output

Case 1: 3Case 2: 4第一次遇到最大团的问题。最大团=原图补图的最大独立集。下面是代码:
#include<iostream>#include<set>#include<map>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;int bg[205][205];int pre[205];bool vis[205];int G,B,M;bool dfs(int u){    vis[u]=1;    for(int i=1; i<=B; i++)    {        if(!vis[i]&&bg[u][i])        {            vis[i]=1;            if(pre[i]==0||dfs(pre[i]))            {                pre[i]=u;                return true;            }        }    }    return false;}int  main(){    //freopen("in.txt","r",stdin);    int x,y;    int loop=1;    while(cin>>G>>B>>M,G||B||M)    {        for(int i=0;i<=G;i++)            fill(bg[i],bg[i]+B+1,1);        memset(pre,0,sizeof(pre));        for(int i=0; i<M; i++)        {            cin>>x>>y;            bg[x][y]=0;        }        int ans=0;        for(int i=1; i<=G; i++)        {            memset(vis,0,sizeof(vis));            if(dfs(i))                ++ans;        }        cout<<"Case "<<loop++<<": "<<G+B-ans<<endl;    }    return 0;}


原创粉丝点击