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

来源:互联网 发布:excel筛选重复数据个数 编辑:程序博客网 时间:2024/06/05 21:32

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

Sample Output

Case 1: 3Case 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 168430090using 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 pr中素材与新建序列不匹配怎么办 淘宝客户收到货不承认要退款怎么办 淘宝包邮店铺 新疆地区拍怎么办 唯品会换货上门但自己寄了怎么办 闲鱼实名认证拍身份证不行怎么办 支付宝绑定的身份证过期了怎么办 实名认证的淘宝店铺被骗走了怎么办 移动宽带los灯亮红灯闪烁怎么办 天猫精灵方糖不支持5g怎么办 天猫精灵方糖连不上网怎么办 设置隐私相片里没什么都没有怎么办 我的手机丢了里面有隐私相片怎么办 苹果5s的id关了怎么办 用op手机快手拍视频打不开怎么办 苹果8p用流量下载不了软件怎么办 流量下载不了内存大的游戏怎么办 苹果6s开机密码忘记了怎么办 咸鱼买东西没寄买家申请退款怎么办 淘宝商品缺货又不想下架怎么办 房产代理商不给我结佣金怎么办 天猫客服遇到脾气不好的客户怎么办 美团外卖点的食品与描述不符怎么办 淘宝极速退款后买家又不想退怎么办 宝贝破损买家退回运费很高怎么办 信而富额度变0了怎么办 申请淘京猫代理时资料填错了怎么办 苹果手机能上网但微信不能用怎么办 卧室壁纸贴的太花了怎么办 贴完墙纸接头处看着是黑色的怎么办 把所有内容放在一张纸上怎么办 孩子脸上被蚊香烧了留下了疤怎么办 微信怎么修改支付密码忘了怎么办 去网鱼网咖上网没带身份证怎么办 淘宝买家号忘了账号和密码怎么办 80多岁的老人前列腺疼怎么办啊 新浪微博发二维码让我入群我怎么办 队友送的太崩了打野应该怎么办 该商品不支持在当前地区销售怎么办 此商品不支持在当前地区销售怎么办 村淘商品在淘宝没展现怎么办 qq音乐不小心充了会员怎么办