Poj 3692(最大匹配)

Kindergarten
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4487   Accepted: 2200

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

2008 Asia Hefei Regional Contest Online by USTC
求最大团,在一般图上是np问题,但对有向无环图上,有定理最大团的大小=补图的最大独立集的大小(这个定理,也是从网上看的,不确定正确性,也不知怎样证明)而这道题的补图就是个二分图了。或者这样理解:转化为补图,补图中有边的说明是不认识的,那么想要剩下的人都认识,就得把边去掉,也就是去掉最少的点使所有的边都去掉,就是最小路径覆盖.
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 1000000000;
const int maxn = 500 + 5; // 单侧顶点的最大数目

// 二分图最大基数匹配,邻接矩阵写法
struct BPM {
  int n, m;               // 左右顶点个数
  int G[maxn][maxn];      // 邻接表
  int left[maxn];         // left[i]为右边第i个点的匹配点编号,-1表示不存在
  bool T[maxn];           // T[i]为右边第i个点是否已标记

  void init(int n, int m) {
    this->n = n;
    this->m = m;
    memset(G, 0, sizeof(G));
  }

  bool match(int u){
    for(int v = 0; v < m; v++) if(G[u][v] && !T[v]) {
      T[v] = true;
      if (left[v] == -1 || match(left[v])){
        left[v] = u;
        return true;
      }
    }
    return false;
  }

  // 求最大匹配
  int solve() {
    memset(left, -1, sizeof(left));
    int ans = 0;
    for(int u = 0; u < n; u++) { // 从左边结点u开始增广
      memset(T, 0, sizeof(T));
      if(match(u)) ans++;
    }
    return ans;
  }

};

BPM solver;

int main(){
    int g,b,m;
    int kase = 0;
    while(scanf("%d%d%d",&g,&b,&m)){
        if(g == 0 && b == 0 && m == 0) break;
        kase++;
        solver.init(g,b);
        for(int i = 0;i < m;i++){
            int x,y;
            scanf("%d%d",&x,&y);x--;y--;
            solver.G[x][y] = 1;
        }
        for(int i = 0;i < g;i++){
            for(int j = 0;j < b;j++){
                solver.G[i][j] = !solver.G[i][j];
            }
        }
        printf("Case %d: %d\n",kase,g+b-solver.solve());
    }
    return 0;
}


你可能感兴趣的:(Poj 3692(最大匹配))