zoukankan      html  css  js  c++  java
  • 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
    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

    题意:给出一张图,求最大团

    最大团=补图的最小独立集

    二分图的最小独立集=点-二分图最大匹配

    我们发现该图的补图是一个二分图 

    所以直接建出该图的补图 跑一边二分图匹配即可

    #define LOCAL
    #include<cstdio>
    #include<string.h>
    using namespace std;
    
    const int maxn = 205;
    int nx, ny;             //x集合和y集合中顶点的个数
    int edge[maxn][maxn];   //edge[i][j]为1表示ij可以匹配
    int cx[maxn], cy[maxn]; //用来记录x集合中匹配的y元素是哪个!
    int visited[maxn];      //用来记录该顶点是否被访问过!
    int path(int u)
    {
        int v;
        for (v = 1; v <= ny; v++)
        {
            if (edge[u][v] && !visited[v])
            {
                visited[v] = 1;
                if (cy[v] == -1 || path(cy[v])) //如果y集合中的v元素没有匹配或者是v已经匹配,但是从cy[v]中能够找到一条增广路
                {
                    cx[u] = v;
                    cy[v] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int maxmatch()
    {
        int res = 0;
        memset(cx, 0xff, sizeof(cx)); //初始值为-1表示两个集合中都没有匹配的元素!
        memset(cy, 0xff, sizeof(cy));
        for (int i = 1; i <= nx; i++)
        {
            if (cx[i] == -1)
            {
                memset(visited, 0, sizeof(visited));
                res += path(i);
            }
        }
        return res;
    }
    
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif
        int m;
        int cas=0;
        while (~scanf("%d%d%d", &nx, &ny, &m))
        {
            cas++;
            if(nx==0&&ny==0&&m==0) break;
            memset(cx,-1,sizeof(cx));
            memset(cy,-1,sizeof(cy));
            memset(visited,0,sizeof(visited));
            for (int i = 1; i <= nx; i++)
            {
                for (int j = 1; j <= ny; j++)
                {
                    edge[i][j] = 1; //初始化该张图
                }
            }
            for (int i = 1; i <= m; i++)
            {
                int tmp1, tmp2;
                scanf("%d%d", &tmp1, &tmp2);
                edge[tmp1][tmp2] = 0;
            }
            printf("Case %d: %d
    ",cas,nx+ny-maxmatch());
        }
        return 0;
    }
  • 相关阅读:
    java面向对象类的继承~ 匿名类 ;多态特性;强制类型转换
    Java面向对象~类和对象&方法,类方法
    1036. Escape a Large Maze
    909. Snakes and Ladders
    559. Maximum Depth of N-ary Tree
    987. Vertical Order Traversal of a Binary Tree
    913. Cat and Mouse
    865. Smallest Subtree with all the Deepest Nodes
    882. Reachable Nodes In Subdivided Graph
    864. Shortest Path to Get All Keys
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852228.html
Copyright © 2011-2022 走看看