POJ3692——Kindergarten (二分图求最小顶点覆盖 (即最大匹配))

解决一个幼儿园游戏选人问题,孩子们分为女孩和男孩两组,女孩间和男孩间互相认识,部分男孩和女孩也互相认识。任务是选择尽可能多的孩子组成游戏团队,确保团队内成员彼此认识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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

大致题意:有g个女孩,b个男孩,女孩子之间互相都认识,男孩子之间也都互相认识,有部分男孩认识部分女孩,现在让你选出一些孩子前提是他们互相都认识,问最多能选多少个孩子。

思路:根据男孩女孩是否认识建立一张图mp[i][j] ,用0,1表示第i个女孩和第j个男孩之间是否认识,0表示认识,1表示不认识。然后我们找出最少的点来覆盖所有不认识的线,这样我们只需删掉其中一半的点即可使得剩下的人都互相认识。求最小顶点覆盖即是求最大匹配。套dfs匈牙利算法求最大匹配。

代码如下

#include <iostream> 
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std; 
#define ll long long int 
int g,b,m;
int mp[210][210];//左到右,女孩到男孩匹配
int vis[210];
int linker[210]; 
bool dfs(int gg)//从左边开始找 
{
    for(int bb=1;bb<=b;bb++)
    if(mp[gg][bb]&&!vis[bb])
    {
        vis[bb]=1;
        if(linker[bb]==-1||dfs(linker[bb]))
        {
            linker[bb]=gg;
            return true;
        }
    }
    return false;
}

int hungary()
{
    int sum=0;

    memset(linker,-1,sizeof(linker));
    for(int i=1;i<=g;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i)) sum++;
    }
    return sum;
}
int main() 
{  
    int cas=0;
    while(scanf("%d%d%d",&g,&b,&m)&&(g+b+m))
    {
        cas++;
        for(int i=1;i<=g;i++)//初始化男女之间都不认识
        for(int j=1;j<=b;j++)
        mp[i][j]=1;

        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x][y]=0;
        }
        int nod=hungary();//找出需要删的点的个数
         printf("Case %d: %d\n",cas,g+b-nod);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值