POJ-3692Kindergarten,求最大独立集!
Time Limit: 2000MS | Memory Limit: 65536K | |
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
题意:在所有男孩都认识彼此与所有女孩都认识彼此的情况下,求一个人数最多的集合使得集合中任意两个人互相认识。
思路:顶点数=最大独立数+最小点覆盖数(最大匹配数);这里我们建立补图,补图有边的说明是不认识的,那么用最少的点(补图的最大二分匹配)将这些边去掉剩下的就全部互相认识了;
const int N=200+10;
int G,B,m,g[N][N],linked[N],v[N];
int dfs(int u)
{
for(int i=1; i<=B; i++)
if(!v[i]&&g[u][i])
{
v[i]=1;
if(linked[i]==-1||dfs(linked[i]))
{
linked[i]=u;
return 1;
}
}
return 0;
}
void hungary()
{
int ans=0;
memset(linked,-1,sizeof(linked));
for(int i=1; i<=G; i++)
{
memset(v,0,sizeof(v));
if(dfs(i)) ans++;
}
printf("%d\n",G+B-ans);
}
int main()
{
int t1=1;
while(~scanf("%d%d%d",&G,&B,&m))
{
if(G==m&&G==B&&B==0) break;
int x,y;
memset(g,-1,sizeof(g));
while(m--)
{
scanf("%d%d",&x,&y);
g[x][y]=0;
}
printf("Case %d: ",t1++);
hungary();
}
return 0;
}
关键在于建立补图然后求出最大二分匹配,只要构图和建模清晰了,剩下的就是裸模板了。
POJ-3692Kindergarten,求最大独立集!的更多相关文章
- poj1419 求最大独立集
题目链接:http://poj.org/problem?id=1419 题意:求最大独立集 思路: 这里有一个定理: 最大独立集=补图的最大团最大团=补图的最大独立集 所以这里我们只要求给出的图的最大 ...
- uva12083 二分图 求最大独立集 转化为求最大匹配 由题意推出二分图
这题大白书例题 : Frank 是一个思想有些保守的高中老师,有一次,他需要带一些学生出去旅行,但又怕其中一些学生在旅途中萌生爱意.为了降低这种事情的发生概率,他决定确保带出去的任意两个学生至少要满足 ...
- Poj(1466),最大独立集,匈牙利算法
题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total S ...
- poj 3692 Kindergarten (最大独立集)
Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4903 Accepted: 2387 Desc ...
- Poj(2771),最大独立集
题目链接:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K To ...
- POJ 1655 求树的重心
POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...
- poj 1966(求点连通度,边连通度的一类方法)
题目链接:http://poj.org/problem?id=1966 思路:从网上找了一下大牛对于这类问题的总结:图的连通度问题是指:在图中删去部分元素(点或边),使得图中指定的两个点s和t不连通 ...
- hdu 1068 Girls and Boys(匈牙利算法求最大独立集)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 3895(求无向图的最大简单环)
题目链接:http://poj.org/problem?id=3895 思想很简单,就是dfs,并且用一个数组记录到该节点所走过的长度,然后如果遇到已经走过的,就说明存在环了, 更新一下ans. /* ...
- POJ 2480 求每一个数对于n的最大公约数的和
这里是枚举每一个最大公约数p,那么最后求的是f(n) = sigma(p*phi(n/p)) phi()为欧拉函数 这里可以试着算一下,然后会发现这个是积性函数的 那么只要考虑每一类质数分开算, ...
随机推荐
- vijos P1426兴奋剂检查 多维费用背包问题的hash
https://vijos.org/p/1426 这是个好题,容易想到用dp[i][v1][v2][v3][v4][v5]表示在前i个物品中,各种东西的容量是那个的时候,能产生的最大价值. 时间不会T ...
- ADO.net数据访问
需要引用对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 //造连接字符串 stri ...
- Spring注解驱动开发之web
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- AJPFX关于throw、throws关键字的解析
throw.throws关键字 throw关键字: 是用于方法体内部,用来抛出一个Throwable类型的异常.如果抛出了检查异常, 则还应该在方法头部声明方法可能抛出的异常类型.该方法的调用者也必须 ...
- P1062 数列
题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… (该序列实际上就是 ...
- js插件定义事件中,this引用的实现模拟
在web项目中,经常会使用jquery和mui等js框架,之前只是按照API说明去按规则使用,比如在jq和mui中,事件处理函数中可以直接用this访问事件源,如下面的代码: <!DOCTYPE ...
- git 学习笔记1
目前我属于粗放型的[学习者],接下来需要做一些改变,让自己更加规范.首先需要学习的就是版本控制系统,本科在工作室的时候使用过一点Subversion,不过到现在已经基本没有印象了.git现在越来越成为 ...
- 阿里云CDN服务功能介绍
- Sql Server中清空所有数据表中的记录
Sql Server中清空所有数据表中的记录 清空所有数据表中的记录: 代码如下:exec sp_msforeachtable @Command1 ='truncate table ?'删除所有数据 ...
- c++通过管道pipe获取cmd输出的字符
#include <stdio.h>#include<iostream>#include<string>using namespace std; // 描述:exe ...