poj--3692--Kindergarten(最大独立集)
2016-02-29 19:22
351 查看
Kindergarten
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
Sample Output
求补图的最大独立集,最大独立集=节点数-最大匹配数
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1010
int pipei[MAXN],used[MAXN],map[MAXN][MAXN];
int G,B,M;
int find(int u)
{
for(int i=1;i<=B;i++)
{
if(!used[i]&&map[u][i]==0)
{
used[i]=1;
if(pipei[i]==-1||find(pipei[i]))
{
pipei[i]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int Case=1;
while(scanf("%d%d%d",&G,&B,&M)!=EOF)
{
if(B==0&&G==0&&M==0)
break;
memset(pipei,-1,sizeof(pipei));
memset(map,0,sizeof(map));
int a,b;
for(int i=0;i<M;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=1;
}
int ans=0;
for(int i=1;i<=G;i++)
{
memset(used,0,sizeof(used));
ans+=find(i);
}
printf("Case %d: %d\n",Case++,B+G-ans);
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6059 | Accepted: 2982 |
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: 3Case 2: 4
求补图的最大独立集,最大独立集=节点数-最大匹配数
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1010
int pipei[MAXN],used[MAXN],map[MAXN][MAXN];
int G,B,M;
int find(int u)
{
for(int i=1;i<=B;i++)
{
if(!used[i]&&map[u][i]==0)
{
used[i]=1;
if(pipei[i]==-1||find(pipei[i]))
{
pipei[i]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int Case=1;
while(scanf("%d%d%d",&G,&B,&M)!=EOF)
{
if(B==0&&G==0&&M==0)
break;
memset(pipei,-1,sizeof(pipei));
memset(map,0,sizeof(map));
int a,b;
for(int i=0;i<M;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=1;
}
int ans=0;
for(int i=1;i<=G;i++)
{
memset(used,0,sizeof(used));
ans+=find(i);
}
printf("Case %d: %d\n",Case++,B+G-ans);
}
return 0;
}
相关文章推荐
- C++ vector 类学习笔记 http://blog.csdn.net/whz_zb/article/details/6827999
- 个人介绍
- [mysqli_escape]mysql转义两次
- poj--3692--Kindergarten(最大独立集)
- Deep Residual Learning 论文解析
- Programing Exercise 4:Neural Networks Learning
- windows资源管理器 已停止工作 循环
- 修复BUG:gulp自动添加版本号修复对CSS中background:url()的匹配问题
- 自我介绍
- 2015年第六届蓝桥杯省赛
- Android Studio 自定义Debug Keystore
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
- 【转】远程结对编程实战:看看别人是怎么做的
- 块和内嵌div和span
- Nginx反向代理的配置
- 【PHP项目部署二】Mysql环境配置
- Xcode里-ObjC, -all_load, -force_load
- 算法代码实现之快速排序,Golang(Go语言)实现
- Rsa 加密与解密
- 操作系统中的IPC机制