POJ - 3692 Kindergarten(最大独立集/最大匹配)

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 ≤ 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

题目大意:有g个女生,和b个男生,女生之间和男生之间都互相认识,而且有m对男女,互相认识。现在老师要建立一个圈子,圈子里面的人都要互相认识,问最多能有多少人。

解答:这个题目是求最大团,对有向无环图上,有定理最大团的大小=补图的最大独立集的大小。

独立集:任意两点都不相连的顶点的集合

定理:最大独立集 = 所有顶点数 - 最小顶点覆盖 = 所有顶点数 - 最大匹配
假如选了一个点就相当于覆盖了以它为端点的所有边。最小顶点覆盖就是选择最少的点来覆盖所有的边。
定理:最小顶点覆盖等于二分图的最大匹配。

完全子图:任意两点都相连的顶点的集合(最大完全子图即最大团)

这个图,建立补图就会发现是一个二分图,找到最大匹配,然后顶点数-最大匹配就是最大独立集。在补图中,所以连边的人都是不认识的,所以我们要去掉所有的边保留最多的点。
代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define re register
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define fi first
#define rep(i,n) for(int i=0;(i)<(n);i++)
#define rep1(i,n) for(int i=1;(i)<=(n);i++)
#define se second

using namespace std;
typedef long long  ll;
typedef unsigned long long  ull;
typedef pair<ll,ll> pii;
const ll mod=998244353;
const ll N =1e3+10;
const double eps = 1e-5;
//const double pi=acos(-1);
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
int dx[4]= {-1,0,1,0}, dy[4] = {0,1,0,-1};
int n,m,g,b;
int dis[N][N];
int st[N],match[N];
//void add(int a,int b)
//{
    //e[idx]=b,ne[idx]=h[a],h[a]=idx++;
   // e[idx]=a,ne[idx]=h[b],c[idx]=0,h[b]=idx++;
//}
//int bfs()
//{
//    FILL(d,-1);
//    queue<int> q;
//    q.push(s);
//    d[s]=0;
//    cur[s]=h[s];
//    while(q.size())
//    {
//         int x=q.front();
//         q.pop();
//         for(int i=h[x];~i;i=ne[i])
//         {
//             int v=e[i];
//             if(d[v]==-1&&c[i])
//             {
//                 cur[v]=h[v];
//                 d[v]=d[x]+1;
//                 if(v==t) return 1;
//                 q.push(v);
//             }
//         }
//    }
//    return 0;
//}
//int find1(int u,int limit)
//{
//    if(u==t) return limit;
//}
//int dinic()
//{
//    int r=0,flow;
//    while (bfs()) while (flow = find1(s, inf)) r += flow;
//    return r;
//}
//void floyd()
//{
//    rep1(i,n)
//        rep1(j,n)
//            rep1(k,n)
//            {
//                if(dis[i][k]&&dis[k][j])
//                {
//                    dis[i][j]=1;
//                    break;
//                }
//            }
//}
int dfs(int u)
{
    rep1(i,b)
    {
        if(dis[u][i]) continue;
        if(st[i]) continue;
        st[i]=1;
        if(match[i]==0||dfs(match[i]))
        {
            match[i]=u;
            return 1;
        }
    }
    return 0;
}
void solve()
{
    int id=1;
    while(cin>>g>>b>>m&&(g+b+m)){
    FILL(dis,0);
    FILL(match,0);
    rep1(i,m)
    {
        int a,b;
        cin>>a>>b;
        dis[a][b]=1;
    }
    int r=0;
    rep1(i,g)
    {
        FILL(st,0);
        r+=dfs(i);
    }
    cout<<"Case "<<id++<<": ";
    cout<<b+g-r<<endl;}
}
int main()
{
    ios
    int T;
    //cin>>T;
    T=1;
    while(T--)
    {
        solve();
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值