二分图的最大团

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
G, B (1 ≤ G, B ≤ 200) and M (0 ≤ MG × 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

题意 : 所有的男生都认识,所有的女生都认识,让你寻找一个最大的一个团,使得其中的所有人之间彼此都认识思路分析 : 题目是让寻找一个二分图的最大团,最大团 = 补图的最大独立集 = 顶点数 - 补图的最小顶点覆盖数 = 顶点数 - 补图的二分图匹配数代码示例 :
#define ll long long
const int maxn = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

bool edge[205][205];
int girl, boy, m;
bool used[205];
int pt[205];

bool dfs(int x){
    for(int i = 1; i <= boy; i++){
        if (edge[x][i] && !used[i]) {
            used[i] = true;
            if (!pt[i] || dfs(pt[i])){
                pt[i] = x;
                return true;
            }
        }
    }
    return false;
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int a, b;
    int kase = 1;
    while(scanf("%d%d%d", &girl, &boy, &m) && girl+boy+m){
        memset(edge, true, sizeof(edge));
        for(int i = 1; i <= m; i++){
            scanf("%d%d", &a, &b);
            edge[a][b] = false;
        }
        int cnt = 0;
        memset(pt, 0, sizeof(pt));
        for(int i = 1; i <= girl; i++){
            memset(used, false, sizeof(used));
            if (dfs(i)) cnt++;
        }
        printf("Case %d: %d\n", kase++, girl+boy-cnt);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ccut-ry/p/8511536.html

时间: 2024-10-13 11:56:15

二分图的最大团的相关文章

Poj_3692 Kindergarten -二分图的最大团

题目:在二分图中找出最大的完全二分图 定理:二分图的最大团=其补图的最大独立集 /************************************************ Author :DarkTong Created Time :2016/7/31 15:17:40 File Name :Poj_3296.cpp *************************************************/ #include <cstdio> #include <cst

poj 3692 Kindergarten,二分图的最大团

最大独立集 = V - 最小顶点覆盖 二分图的最小顶点覆盖数 = 最大匹配数 最大团 = 补图的最大独立集 #include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <vector> #include <queue> using namespace std; const int maxn = 200 + 10; int n,

二分图的最小顶点覆盖 最大独立集 最大团

二分图的最小顶点覆盖 定义:假如选了一个点就相当于覆盖了以它为端点的所有边.最小顶点覆盖就是选择最少的点来覆盖所有的边. 方法:最小顶点覆盖等于二分图的最大匹配. 我们用二分图来构造最小顶点覆盖. 对于上面这个二分图,顶点分为左右两个集合,X集合包含1,2,3,4,Y集合包含5,6,7,8,9.假如现在我们已经找到一个最大匹配M,就是上面的红线所标注的M={(1,7),(2,5),(4,8)}.我们作如下定义:(1)定义1.2.4.5.7.8为已经匹配过的点,其他点为未匹配的点:(2)定义(4,

hdu 5556 Land of Farms 最大团+暴力

Land of Farms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 539    Accepted Submission(s): 177 Problem Description Farmer John and his brothers have found a new land. They are so excited and d

Gym - 101915D Largest Group 最大团

给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bits/stdc++.h> using namespace std; #define N 50 int useif[N]; //记录y中节点是否使用 0表示没有访问过,1为访问过 int link[N]; //记录当前与y节点相连的x的节点 int mat[N][N]; //记录连接x和y的边,如果i和j之

二分图&amp;&amp;匈牙利算法(二分图基本算法)

二分图 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图. 区别二分图,关键是看点集是否能分成两个独立的点集.如下图所示 二分图的最大匹配 给定一个二分图G,在G的一个子图M中,M的边集中的任意两条边都不依附于同一个顶点,则称M是一个匹配. 选择这样的边数最大的子集称为图的最大匹配问题(maxim

POJ 3692 Kindergarten (二分图 最大团)

Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5660   Accepted: 2756 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

[HDU2458]Kindergarten(二分图匹配,最大团)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2458 题意:n男m女构成二分图,希望找出k个人,使他们互相都认识. 相当于求这个图的最大团,由于特殊性质(是个二分图),因此可以可以用一个定理: 最大团 = 补图的最大独立集 其实想一下这个定理也很合理. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 210; 5 int nu, nv; 6 int G

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 e