Kindergarten
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 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 |
[Submit] [Go Back] [Status] [Discuss]
就是让你求二分图里的最大团, 因为本身男生之间互相认识, 女生之间也互相认识. 作补图之后求补图的最大独立集即可.
关于最大团的概念以及求法: Click.
#include<stdio.h>
#include<bitset>
#include<cstring>
#define clear(a) memset(a, 0, sizeof(a))
using namespace std;
const int maxn = 405;
bitset<maxn> vis;
int g, b, m, cas, num, ans;
int match[maxn], mp[maxn][maxn], h[maxn];
struct edge{ int nxt, v;} e[maxn * maxn];
inline void add(int u, int v){e[++num].v = v, e[num].nxt = h[u], h[u] = num;}
bool find(int u){
for(int i = h[u]; i; i = e[i].nxt){
int v = e[i].v;
if(!vis[v]){
vis[v] = true;
if(!match[v] || find(match[v]))
return match[v] = u;
}
}
return false;
}
int main(){
while(scanf("%d%d%d", &g, &b, &m), g + b + m){
int x, y;
for(int i = 1; i <= m; ++i) scanf("%d%d", &x, &y), mp[x][y] = true;
for(int i = 1; i <= g; ++i)
for(int j = 1; j <= b; ++j)
if(!mp[i][j]) add(i, j);
for(int i = 1; i <= g; ++i){
if(find(i)) ans ++;
vis = 0;
}
printf("Case %d: %d\n", ++cas, g + b - ans);
num = ans = 0;
clear(h), clear(mp), clear(match);
}
}