Time Limit: 2000MS | Memory Limit: 65536K | |
Total 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 ≤ 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对男女,他们互相认识。 求最大的人数集合,集合里的人都相互认识。相关概念定理:
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; }