Kindergarten
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

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

题意:在所有男孩都认识彼此与所有女孩都认识彼此的情况下,求一个人数最多的集合使得集合中任意两个人互相认识。

思路:顶点数=最大独立数+最小点覆盖数(最大匹配数);这里我们建立补图,补图有边的说明是不认识的,那么用最少的点(补图的最大二分匹配)将这些边去掉剩下的就全部互相认识了;

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,求最大独立集!的更多相关文章

  1. poj1419 求最大独立集

    题目链接:http://poj.org/problem?id=1419 题意:求最大独立集 思路: 这里有一个定理: 最大独立集=补图的最大团最大团=补图的最大独立集 所以这里我们只要求给出的图的最大 ...

  2. uva12083 二分图 求最大独立集 转化为求最大匹配 由题意推出二分图

    这题大白书例题 : Frank 是一个思想有些保守的高中老师,有一次,他需要带一些学生出去旅行,但又怕其中一些学生在旅途中萌生爱意.为了降低这种事情的发生概率,他决定确保带出去的任意两个学生至少要满足 ...

  3. Poj(1466),最大独立集,匈牙利算法

    题目链接:http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  4. poj 3692 Kindergarten (最大独立集)

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4903   Accepted: 2387 Desc ...

  5. Poj(2771),最大独立集

    题目链接:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K To ...

  6. POJ 1655 求树的重心

    POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...

  7. poj 1966(求点连通度,边连通度的一类方法)

    题目链接:http://poj.org/problem?id=1966 思路:从网上找了一下大牛对于这类问题的总结:图的连通度问题是指:在图中删去部分元素(点或边),使得图中指定的两个点s和t不连通  ...

  8. hdu 1068 Girls and Boys(匈牙利算法求最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. poj 3895(求无向图的最大简单环)

    题目链接:http://poj.org/problem?id=3895 思想很简单,就是dfs,并且用一个数组记录到该节点所走过的长度,然后如果遇到已经走过的,就说明存在环了, 更新一下ans. /* ...

  10. POJ 2480 求每一个数对于n的最大公约数的和

    这里是枚举每一个最大公约数p,那么最后求的是f(n) = sigma(p*phi(n/p))    phi()为欧拉函数 这里可以试着算一下,然后会发现这个是积性函数的 那么只要考虑每一类质数分开算, ...

随机推荐

  1. Magento Order 状态详解

    流程图:

  2. PHP 官方说明

    http://php.net/manual/en/mysqli.affected-rows.php The above examples will output: Affected rows (INS ...

  3. unix shell 解析 1

    ---- shell 1 testdb3:/home/oracle [pprod] >more /home/oracle/utility/macro/tns_log_back_12c.sh #! ...

  4. 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球

    在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...

  5. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  6. oracle数据库常用的99条查询语句

    1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...

  7. (七)Mybatis总结之注解开发

    请移步到 https://www.cnblogs.com/lxnlxn/p/5996707.html

  8. contact用法解析

    经典用法: mysql> select concat('11','22','33'); +------------------------+ | concat('11','22','33') | ...

  9. apm - 查询高级电源管理(APM) BIOS

    总览 apm [ - vVmsS ] 描述 apm 读取 /proc/apm 并用人能看懂的格式输出.因为提供了首要的电池状态,这个命令在有兼容的 APM BIOS 的笔记本电脑上非常有用. apm ...

  10. Java Servlet 非英文乱码

    response.setHeader("Content-Type", "text/json; charset=UTF-8"); request.setChara ...