图论 二分图 最大团

Kindergarten

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

首先有几个概念:


1.最大独立集 
      独立集是指图G中两两互不相邻的顶点构成的集合。
  例如:有个图有n个结点,y条边任选图中一个顶点,把它染成黑色,则和它相连的顶点必须都被染成白色,
      但与被染成白色的顶点相连的顶点可以被染成白色也可以被染成黑色
    问:这个图最多有多少个顶点能被染成黑色?


2.最大匹配数,最小顶点覆盖
  二分图的|最小点集|=|最大匹配| 


3.最大独立集=顶点数-最大匹配边数(二分图当然也成立)


4.最大团 = 原图补图的最大独立集


所以 最大团 = 补图的顶点数 - 最大匹配数(匈牙利匹配)


typedef int weight_t;
/**
 *二分图匹配(匈牙利算法的DFS实现)
 *Graph[][]两边定点划分的情况
 *CALL:ret=hungary();输出最大匹配数
 *优点:适于稠密图,DFS找增广路快,实现简洁易于理解
 *时间复杂度:O(VE);
 *gn gm 为两个集合的大小,从0 ~ n-1 编号
 *********
  初始化Graph 为0;
 *********
 */
const int SIZE = 510;
// 存储二分图 左边的集合跟右边集合边的信息
// 如果有边为 1 , 没边为 0
weight_t Graph[SIZE][SIZE];

int Link[SIZE]; //对于右边集合的 i, link[i]即对应左边集合的 node
bool vis[SIZE];
int gn,gm;

//对于左边的集合的node节点 求增广路径
bool DFS( int node){
    //对右边集合的每个结点便利一次
    for (int i = 0;i < gm;++i){

        //如果已经找过 或者没有这个边
        if ( vis[i] || !Graph[node][i])continue;

        vis[i] = true;
        //如果这个点上未盖点 或者该点还能找到增广路径
        if ( Link[i] == -1 || DFS( Link[i] ) ){
            Link[i] = node;
            return true;
        }
    }
    return false;
}
int hungary(){
    int ret = 0;
    memset(Link , -1 ,sizeof(Link));
    //对左边集合的每个点求增广路径
    for (int u = 0;u < gn;++u){
        memset(vis,0,sizeof(vis));
        if (DFS(u)) ret++;
    }
    return ret;
}
int main(){
    int g,b,m;
    int kase = 1;
    while( scanf("%d%d%d",&g,&b,&m) != EOF ){
        if ( g + b + m == 0 ) break;
        gn = g , gm = b;
        for (int i = 0;i < gn;++i)
            for (int j = 0;j < gm;++j)
                Graph[i][j] = 1;
        //memset(Graph,0,sizeof(Graph));
        int x,y;
        while( m-- ){
            scanf("%d%d",&x,&y);
            Graph[x-1][y-1] = 0;
        }
        //cout<<hungary()<<endl;
        printf("Case %d: %d\n",kase++,gn+gm-hungary());
    }
    return 0;
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值