您的位置:首页 > 其它

hdu2458(二分图最小点覆盖、最大独立集)

2018-02-12 19:03 330 查看


Kindergarten

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1346    Accepted Submission(s): 714


Problem 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

题意:有g个女生,b个男生,女生和女生互相都认识,男生和男生互相都认识,男生女生有m对互相认识,现在要找出最大的一群学生使得他们互相都认识。

思路:先构造补图,相互不认识的人就连一条边,在补图中寻找最小点覆盖,那么去掉这些点之后补图中所有的边也就没有了,即原图中所有点都互相直接相连。而这个最小点覆盖就等于补图二分图的最大匹配。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
const int N = 505;
bool map[205][205];
bool vis
;
int link
,head
;
int cnt,n;
struct Edge
{
int to;
int next;
};
Edge edge[N*N];
void Init()
{
cnt = 0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
bool dfs(int u)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v = edge[i].to;
if(!vis[v])
{
vis[v] = 1;
if(link[v] == -1 || dfs(link[v]))
{
link[v] = u;
return true;
}
}
}
return false;
}
int match()
{
int ans = 0;
memset(link,-1,sizeof(link));
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
int g,b,m;
int cas=0;
while(~scanf("%d%d%d",&g,&b,&m)&&(g||b||m))
{
Init();
n=g+b;
memset(map,0,sizeof(map));
int x,y;
while(m--)
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
for(int i=1;i<=g;i++)
{
for(int j=1;j<=b;j++)
{
if(!map[i][j])
{
add(i-1,g+j-1);
add(g+j-1,i-1);
}
}
}
printf("Case %d: %d\n",++cas,n-match()/2);
}
}

很多问题都可以转化为二分图匹配模型。二分图有如下几种常见变形:



(1)二分图的最小顶点覆盖 

最小顶点覆盖要求用最少的点(X或Y中都行),让每条边都至少和其中一个点关联。

Knoig定理:二分图的最小顶点覆盖数等于二分图的最大匹配数。



(2)DAG图的最小路径覆盖 

用尽量少的不相交简单路径覆盖有向无环图(DAG)G的所有顶点,这就是DAG图的最小路径覆盖问题。

结论:DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)



(3)二分图的最大独立集

最大独立集问题: 在N个点的图G中选出m个点,使这m个点两两之间没有边.求m最大值

结论:二分图的最大独立集数 = 节点数(n)— 最大匹配数(m)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐