poj3690——Kindergarten(最大独立点集,匈牙利算法)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了poj3690——Kindergarten(最大独立点集,匈牙利算法),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2521字,纯文字阅读大概需要4分钟。
内容图文
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
题意是一群男生互相认识,一群女生互相认识,还有一些男生认识一些女生。现在要找出一些人,他们都互相认识,求可以找出的人的最大数。
可以抽象成找满足互相有边的点的最大数量,我们可以求反问题,在补图中,互相不联系的点集正好就是原图中互相联系的点集,求互不联系的点集问题正好就是求最大独立集问题,对匈牙利算法稍加修改就行
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
#include <cstring>
#include <string>
#define MAXN 210
#define inf 0xffffffff
using
namespace
std;
intmap[MAXN][MAXN],vis[MAXN],pre[MAXN];
int g,b;
int find(int cur)
{
for(int i=1; i<=b; ++i)
{
if(!vis[i]&&!map[cur][i]) //map求反是关键!因为是要求补图的最大独立集
{
vis[i] = true;
if(pre[i] == 0 || find(pre[i]))
{
pre[i] = cur;
return1;
}
}
}
return0;
}
int main()
{
int m,x,y,cnt=1;
while(~scanf("%d%d%d",&g,&b,&m))
{
memset(map,0,sizeof(map));
memset(pre,0,sizeof(pre));
if(g==0&&b==0&&m==0)
break;
for(int i=1; i<=m; ++i)
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
int ans=g+b;
for(int i=1; i<=g; ++i)
{
memset(vis,0,sizeof(vis));
if(find(i))
ans--;
}
printf("Case %d: %d\n",cnt++,ans);
}
return0;
}
原文:http://blog.csdn.net/blue_skyrim/article/details/51329232
内容总结
以上是互联网集市为您收集整理的poj3690——Kindergarten(最大独立点集,匈牙利算法)全部内容,希望文章能够帮你解决poj3690——Kindergarten(最大独立点集,匈牙利算法)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。