zoukankan      html  css  js  c++  java
  • poj 3692 Kindergarten

    Kindergarten
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6956   Accepted: 3436

    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

    翻译:小朋友手拉手做游戏,现在老师希望从小朋友当中挑出一个集合,集合中的小朋友全都认识,那么这个集合最大是多少。
    思路:显然是个最大独立集问题,只是男女小朋友关系图的补图的最大独立集,其补图中两个人相互连线意味着相互不认识,从图中挑出来的最大独立集就是题设要求的。
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<set>
    #include<vector>
    #include<cstring>
    #include<string>
    using namespace std;
    #define INF 0x3f3f3f3f
    const int N_MAX = 500;
    int V;//点的个数
    vector<int>G[N_MAX];
    int match[N_MAX];
    bool used[N_MAX];
    void add_edge(int u, int v) {
        G[u].push_back(v);
        G[v].push_back(u);
    }
    
    bool dfs(int v) {
        used[v] = true;
        for (int i = 0; i < G[v].size(); i++) {
            int u = G[v][i], w = match[u];
            if (w < 0 || !used[w] && dfs(w)) {
                match[v] = u;
                match[u] = v;
                return true;
            }
        }
        return false;
    }
    
    int bipartite_matching() {
        int res = 0;
        memset(match, -1, sizeof(match));
        for (int v = 0; v < V; v++) {
            if (match[v] < 0) {
                memset(used, 0, sizeof(used));
                if (dfs(v))
                    res++;
            }
        }
        return res;
    }
    bool vis[N_MAX][N_MAX];
    int g, B, M;
    int main() {
        int Case = 0;
        while (scanf("%d%d%d",&g,&B,&M)&&g) {
            Case++;
            //0~g-1:girl
            //g~g+B-1:boy
            V = g + B;
            memset(vis,0,sizeof(vis));
            for (int i = 0; i < M; i++) {
                int a, b;
                scanf("%d%d",&a,&b);
                a--, b--;
                vis[a][g + b]=true;
            }
            for (int i=0; i<g;i++) {
                for (int j = g; j < V;j++) {
                    if (!vis[i][j]) {//建立原图的补图
                        add_edge(i,j);
                    }
                }
            }
            printf("Case %d: %d
    ",Case,V-bipartite_matching());
            for (int i = 0; i < V;i++) {
                G[i].clear();
            }
        }
        return 0;
    }
  • 相关阅读:
    36、基于TCP、UDP协议的嵌套字通信
    34、异常以及网络编程
    作业4月15号
    31、反射与内置方法、元类
    30、多态与鸭子类型以及内置函数
    作业4月9号
    29、继承
    作业4月8号
    28、封装
    27、面向对象
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/7223837.html
Copyright © 2011-2022 走看看