Poj 3692(最大匹配)

本文探讨了最大团问题及其在补图上的应用,详细解释了如何将问题转化为二分图最大基数匹配问题,并提供了求解算法。通过实例演示,帮助读者理解如何在一般图上寻找最大团的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

求最大团,在一般图上是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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值