Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4788 | Accepted: 2327 |
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 (1 ~ 200)和 B (1 ~ 200)分别是女孩的个数和男孩的个数。后给出 M 对关系 G 对 B,代表 G 号女孩认识 B 号男孩,女孩与女孩之间相互认识,男孩与男孩之间相互认识。求最大的人数,使里面的任意两个人之间都相互认识。
思路:
二分图最大匹配。匈牙利算法。给出的关系是相互认识的关系,那么求出来的最大独立集代表的就是里面的人相互不认识。如果给出的是相互不认识的关系,那么求出来的最大独立集就是里面的人相互都认识。故将图反过来建,求其最大独立集即可。
AC:
#include <cstdio> #include <string.h> using namespace std; int vn,un; int w[205][205],vis[205],linker[205]; bool dfs(int u) { for(int v = 1;v <= vn;v++) if(w[u][v] && !vis[v]) { vis[v] = 1; if(linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } return false; } int hungary() { int res = 0; memset(linker,-1,sizeof(linker)); for(int u = 1;u <= un;u++) { memset(vis,0,sizeof(vis)); if(dfs(u)) res++; } return res; } int main() { int m,ans = 0; while(~scanf("%d%d%d",&un,&vn,&m) && (un + vn + m)) { ans++; for(int i = 1;i <= un;i++) for(int j = 1;j <= vn;j++) w[i][j] = 1; while(m--) { int f,t; scanf("%d%d",&f,&t); w[f][t] = 0; } printf("Case %d: ",ans); printf("%d\n",vn + un - hungary()); } return 0; }