zoukankan      html  css  js  c++  java
  • The Accomodation of Students(二分图判断+匈牙利)

    The Accomodation of Students
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. 

    Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room. 

    Calculate the maximum number of pairs that can be arranged into these double rooms. 
     

    Input

    For each data set: 
    The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs. 

    Proceed to the end of file. 

     

    Output

    If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms. 
     

    Sample Input

    4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
     

    Sample Output

    No 3


    当做有向图建图:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define MAXN 201
    using namespace std;
    int Map[MAXN][MAXN];
    int color[MAXN];//染色
    int N, M;
    bool judge(int u)//判断是否是二分图
    {
        for(int i = 1; i <= N; i++)
        {
            if(Map[u][i] == 0 && Map[i][u] == 0) continue;  ///注意判断条件,因为这是建的有向图了,因为在一般的二分图匹配的过程中是有向图和
                                                            ///无向图都一样的,因为用的是match都做标记了,但是在这里dfs判断的时候就必须不一样了
            if(color[u] == color[i]) return false;
            if(!color[i])
            {
                color[i] = 3 - color[u];
                if(!judge(i))
                    return false;
            }
        }
        return true;
    }
    int match[MAXN];
    bool used[MAXN];
    int DFS(int u)
    {
        for(int i = 1; i <= N; i++)
        {
            if(Map[u][i] == 0) continue;
            if(!used[i])
            {
                used[i] = true;
                if(match[i] == -1 || DFS(match[i]))
                {
                    match[i] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
    void solve()
    {
        memset(Map, 0, sizeof(Map));
        int a, b;
        for(int i = 1; i <= M; i++)
        {
            scanf("%d%d", &a, &b);
            Map[a][b] = 1;
        }
        memset(color, 0, sizeof(color));
        color[1] = 1;
        if(!judge(1))
        {
            printf("No
    ");
            return ;
        }
        int ans = 0;
        memset(match, -1, sizeof(match));
        for(int i = 1; i <= N; i++)
        {
            memset(used, false, sizeof(used));
            ans += DFS(i);
        }
        printf("%d
    ", ans); 
    int main()
    {
        while(scanf("%d%d", &N, &M) != EOF)
        {
            solve();
        }
        return 0;
    }
    


    当做无向图:
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define MAXN 201
    using namespace std;
    int Map[MAXN][MAXN];
    int color[MAXN];//染色
    int N, M;
    bool judge(int u)//判断是否是二分图
    {
        for(int i = 1; i <= N; i++)
        {
            if(Map[u][i] == 0) continue;
            if(color[u] == color[i]) return false;
            if(!color[i])
            {
                color[i] = 3 - color[u];
                if(!judge(i))
                    return false;
            }
        }
        return true;
    }
    int match[MAXN];
    bool used[MAXN];
    int DFS(int u)
    {
        for(int i = 1; i <= N; i++)
        {
            if(Map[u][i] == 0) continue;
            if(!used[i])
            {
                used[i] = true;
                if(match[i] == -1 || DFS(match[i]))
                {
                    match[i] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
    void solve()
    {
        memset(Map, 0, sizeof(Map));
        int a, b;
        for(int i = 1; i <= M; i++)
        {
            scanf("%d%d", &a, &b);
            Map[a][b] = Map[b][a] = 1;
        }
        memset(color, 0, sizeof(color));
        color[1] = 1;
        if(!judge(1))
        {
            printf("No
    ");
            return ;
        }
        int ans = 0;
        memset(match, -1, sizeof(match));
        for(int i = 1; i <= N; i++)
        {
            memset(used, false, sizeof(used));
            ans += DFS(i);
        }
        printf("%d
    ", ans / 2); //////对两个部里的都匹配了,这样就相当于匹配了两次了
    }
    int main()
    {
        while(scanf("%d%d", &N, &M) != EOF)
        {
            solve();
        }
        return 0;
    }
    









  • 相关阅读:
    Unicode与JavaScript详解 [很好的文章转]
    4种方法生成二维码 (js 控制canvas 画出 二维码)
    JQuery-Dialog(弹出窗口,遮蔽窗口)
    JQuery实现可编辑的表格
    7个提高效率的JavaScript调试工具
    jQuery中的编程范式
    10款CSS3按钮
    jQuery(function(){})与(function(){})(jQuery)的区别
    JQuery之ContextMenu(右键菜单)
    糟糕的css用法 1
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717980.html
Copyright © 2011-2022 走看看