CUGB图论专场2:C - Kindergarten 最大完全子图
来源:互联网 发布:excel筛选重复数据个数 编辑:程序博客网 时间:2024/06/05 21:32
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 <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;}
- CUGB图论专场2:C - Kindergarten 最大完全子图
- poj -- 3692 Kindergarten (最大完全子图)
- CUGB图论专场2:B - Asteroids 二分图:最小顶点覆盖=最大匹配数
- CUGB图论专场2:D - Power Network 最大流EK算法
- CUGB图论专场2:E - Drainage Ditches 最大流EK算法与Dinic算法
- CUGB图论专场:C - Tangled in Cables(最小生成树kruscal)
- CUGB图论专场2:A - Antenna Placement 二分图最小边覆盖
- CUGB图论专场2:F - PIGS 需要调整的网络流
- CUGB图论专场2:The Bottom of a Graph 强连通Tarjan算法
- CUGB图论专场2:J - Network of Schools (Tarjan缩点)
- POJ 2458 Kindergarten( 最大独立集(图) & 完全子图 )
- HDU-2458 Kindergarten 最大完全子图(最大团)中顶点的个数
- CUGB图论专场2:I - Popular Cows 求受所有牛仰慕的牛(Tarjan缩点)
- CUGB图论专场:D - Command Network(最小树形图:朱刘算法)
- CUGB图论专场:J - Transfer water(最小树形图)
- CUGB图论专场:A - Domino Effect(最短路径:dijkstra算法)
- CUGB图论专场:B - ROADS(邻接表+dijkstra优先队列优化)
- CUGB图论专场:F - Cow Marathon(邻接表+dfs的最长继承)
- ASP.Net网站几个部署问题
- poj1716 Integer Intervals(贪心)
- Java学习第14天:集合框架零接触和基本理解(List和Set)
- 天猫魔盒 + 旧显示器 DIY网络电视
- Java学习第15-16天:集合框架之Map和泛型(半壁江山了)
- CUGB图论专场2:C - Kindergarten 最大完全子图
- RAS理解
- eclipse九个最常用的快捷键
- 面试过程要做的准备
- Ubuntu 下安装Tomcat 6
- “大气”网站欣赏
- 安德的游戏--leader管理团队
- 算法入门经典第八章学习笔记(下)
- 0.driverbase-IRP、IO_STACK_LOCATION、文件三种读写方式(buffer/driect/other)、DeviceIoControl