poj-3692(最大完全子图)

来源:互联网 发布:中科院心理研究所知乎 编辑:程序博客网 时间:2024/06/05 16:32
Kindergarten
Time Limit: 2000MS
Memory Limit: 65536KTotal Submissions: 5335
Accepted: 2602

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 ≤ MG × 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 31 11 22 32 3 51 11 22 12 22 30 0 0

Sample Output

Case 1: 3Case 2: 4题意: 有g个女孩,她们互相认识;有b个男孩,他们互相认识;有m对男女,他们互相认识。求最大的人数集合,集合里的人都相互认识。

相关概念定理:

1.独立集:任意两点都不相连的顶点的集合 

2.定理:最大独立集=顶点数-最大匹配边数(二分图当然也成立)

3.完全子图:任意两点都相连的顶点的集合(最大完全子图即最大团)  

4.定理:最大团=原图补图的最大独立集

所以根据第四条可以看出在二分图中最大团问题可以方便解决,但在一般图中为NP问题。

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int line[510][510];int girl[510];int used[510];int x,y,g,b,m;bool find(int z){for(int i=1;i<=g;i++){if(line[z][i]&&!used[i]){used[i]=1;if(!girl[i]||find(girl[i])){girl[i]=z;return true;}}}return false;}int main(){int t=0;while(scanf("%d%d%d",&g,&b,&m)&&g&&b&&m){int sum=0;memset(girl,0,sizeof(girl));for(int i=1;i<=b;i++)for(int j=1;j<=g;j++){line[i][j]=1;}for(int z=1;z<=m;z++){scanf("%d%d",&x,&y);line[y][x]=0;}for(int a=1;a<=b;a++){memset(used,0,sizeof(used));if(find(a)) sum++;}printf("Case %d: ",++t);printf("%d\n",b+g-sum);}return 0;}


0 0