POJ3692 Kindergarten —— 二分图最大团

题目链接:http://poj.org/problem?id=3692

Kindergarten
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7371   Accepted: 3636

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
GB (1 ≤ GB ≤ 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

Source

题解:

最大团 = 总体 - 补图的最大独立集。

证明:最大独立集,即两两没有联系。那么它的补图,就是两两都有联系。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 const int INF = 2e9;
14 const int MOD = 1e9+7;
15 const int MAXN = 200+10;
16 
17 int uN, vN;
18 int M[MAXN][MAXN], link[MAXN];
19 bool vis[MAXN];
20 
21 bool dfs(int u)
22 {
23     for(int i = 1; i<=vN; i++)
24     if(M[u][i] && !vis[i])
25     {
26         vis[i] = true;
27         if(link[i]==-1 || dfs(link[i]))
28         {
29             link[i] = u;
30             return true;
31         }
32     }
33     return false;
34 }
35 
36 int hungary()
37 {
38     int ret = 0;
39     memset(link, -1, sizeof(link));
40     for(int i = 1; i<=uN; i++)
41     {
42         memset(vis, 0, sizeof(vis));
43         if(dfs(i)) ret++;
44     }
45     return ret;
46 }
47 
48 int main()
49 {
50     int T, m, kase = 0;
51     while(scanf("%d%d%d", &uN, &vN, &m)&& (uN||vN||m))
52     {
53         memset(M, true, sizeof(M));
54         for(int i = 1; i<=m; i++)
55         {
56             int u, v;
57             scanf("%d%d", &u, &v);
58             M[u][v] = false;
59         }
60 
61         int cnt = hungary();
62         printf("Case %d: %d
", ++kase, uN+vN-cnt);
63     }
64 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7811313.html