zoukankan      html  css  js  c++  java
  • hdu 2444(染色法判断二分图+最大匹配)

    The Accomodation of Students

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5225    Accepted Submission(s): 2374


    Problem 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
     
    Source
     
    题意:给出 n 个学生,将这些学生互不认识的人分成一组,剩下的人分成另一组,然后在这两组中互相认识的人配对,问最多能够配多少对?
    题解:先用染色法对无向图进行判断是否为二分图,不是的话直接输出No,是的话进行最大匹配。网上有很多解法其实是有问题的,题目并没有说是强连通图,都是从1开始判断,但是这组数据就是过不了的,所以我们要对所有点进行判断。
    无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶数。
    4 3
    2 3
    3 4
    4 2
    ans:No
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include <algorithm>
    #include <math.h>
    #include <queue>
    using namespace std;
    const int N = 205;
    int n,m;
    int graph[N][N],mp[N][N];
    int linker[N];
    bool vis[N];
    int color[N]; ///染色数组
    bool dfs(int u){
        for(int v=1;v<=n;v++){
            if(graph[u][v]&&!vis[v]){
                vis[v] = true;
                if(linker[v]==-1||dfs(linker[v])){
                    linker[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    bool bfs(int s){
        queue<int> q;
        color[s] = 0;
        q.push(s);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(int i=1;i<=n;i++){
                if(graph[u][i]){
                    if(color[i]==-1){///未染色
                        color[i] = !color[u];
                        q.push(i);
                    }else{
                        if(color[i]==color[u]) return false;
                    }
                }
            }
        }
        return true;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF){
            memset(graph,0,sizeof(graph));
            memset(color,-1,sizeof(color));
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                graph[u][v] = 1;
                graph[v][u] = 1;
            }
            bool flag = false;
            for(int i=1;i<=n;i++){
                if(color[i]!=-1) continue; ///已染色
                if(!bfs(i)) {
                    flag = true;
                    break;
                }
            }
            if(flag){
                printf("No
    ");
                continue;
            }
            memset(linker,-1,sizeof(linker));
            int res = 0;
            for(int i=1;i<=n;i++){
                memset(vis,false,sizeof(vis));
                if(dfs(i)){
                    res++;
                }
            }
            printf("%d
    ",res/2);
        }
        return 0;
    }
  • 相关阅读:
    python之json&pickle
    python之装饰器
    软件测试基础
    软件测试分类
    python3文件的读写操作
    python3对excel文件读写操作
    Java集合整理
    mybatis一对多关系的关联查询
    用xftp从win7系统传输一些必要的文件到Linux
    Spring和Mybatis的整合
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5717226.html
Copyright © 2011-2022 走看看