CUGB图论专场2:C - Kindergarten 最大完全子图

C - Kindergarten
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

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 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 <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define MM 10002
#define MN 505
#define INF 168430090
using namespace std;
typedef long long ll;
int n,m,k,t=1,u,v,sum,pre[MN],vis[MN],Map[MN][MN];
int dfs(int x)
{
    for(int y=1; y<=m; y++)
    {
        if(!vis[y]&&!Map[x][y])  //求的是!Map[x][y]的,即求反面的,即就是互相不认识的了
        {
            vis[y]=1;
            if(pre[y]==0||dfs(pre[y]))
            {
                pre[y]=x;
                return 1;
            }
        }
    }
    return 0;
}
void search()
{
    for(int x=1; x<=n; x++)
    {
        mem(vis,0);
        sum+=dfs(x); //匹配求两两互相不认识的
    }
}
int main()
{
    while(cin>>n>>m>>k&&(n||m||k))
    {
        sum=0; mem(Map,0); mem(pre,0);
        while(k--)
        {
            scanf("%d%d",&u,&v);
            Map[u][v]=1;
        }
        search();
        printf("Case %d: %d\n",t++,n+m-sum);
    }
    return 0;
}


你可能感兴趣的:(CUGB图论专场2:C - Kindergarten 最大完全子图)