码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3692 Kindergarten

时间:2016-08-17 13:51:52      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

 

Kindergarten
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6345   Accepted: 3129

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

 

技术分享

 

 二分图最大独立集

这道题目要逆向思维,嚯嚯。

在没有关系的男女间加边,然后求最大独立集

最大独立集=点数-最大匹配数(最小点覆盖)

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxn=405;
 8 const int maxm=40005;
 9 int B,G,M;
10 bool g[maxn][maxn];
11 int Head[maxn],Next[maxm],Adj[maxm];
12 int c,cnt;
13 bool use[maxn];
14 int match[maxn];
15 
16 void AddEdge(int u,int v)
17 {
18     c++;
19     Adj[c]=v;
20     Next[c]=Head[u];
21     Head[u]=c;
22 }
23 
24 bool dfs(int y)
25 {
26     for(int i=Head[y];i;i=Next[i])
27     {
28         int x=Adj[i];
29         if(use[x]) continue;
30         use[x]=true;
31         if(match[x]==0||dfs(match[x]))
32         {
33             match[x]=y;
34             return true;
35         }
36     }
37     return false;
38 }
39 
40 void hungarian()
41 {
42     for(int i=1;i<=B;i++)
43     {
44         memset(use,false,sizeof(use));
45         if(dfs(i)) cnt++;
46     }
47 }
48 
49 int main()
50 {
51     freopen("poj3692.in","r",stdin);
52     freopen("poj3692.out","w",stdout);
53     int t=0;
54     while(scanf("%d %d %d",&G,&B,&M)!=EOF)
55     {
56         if(G==0&&B==0&&M==0) break;
57         t++;cnt=0;
58         memset(g,false,sizeof(g));
59         memset(match,0,sizeof(match));
60         c=0;
61         memset(Head,0,sizeof(Head));
62         memset(Next,0,sizeof(Next));
63         memset(Adj,0,sizeof(Adj));
64         for(int i=1;i<=M;i++)
65         {
66             int a,b;
67             scanf("%d %d",&a,&b);
68             g[a][b]=true;
69         }
70         for(int i=1;i<=G;i++)
71             for(int j=1;j<=B;j++)
72                 if(!g[i][j]) AddEdge(j,i+200);
73         hungarian();
74         printf("Case %d: %d\n",t,B+G-cnt);
75     }
76     return 0;
77 }
View Code

 

POJ3692 Kindergarten

标签:

原文地址:http://www.cnblogs.com/cnblogsLSY/p/5779525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!