[图论][二分图判断]The Accomodation of Students

Posted lz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[图论][二分图判断]The Accomodation of Students相关的知识,希望对你有一定的参考价值。

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
 
思路:判断是否为二分图:在无向图G中,如果存在奇数回路(回路中节点个数为奇数),则不是二分图。否则是二分图。
          染色法判断回路奇偶性:把相邻两点染成黑白两色,如果相邻两点出现颜色相同则存在奇数回路。也就是非二分图。
AC代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int n,m;
int Map[210][210];
int flag[210];
int used[210];
int vis[210];

bool bfs_judge(){//染色法判断是否为二分图
  for(int i=0;i<210;i++) flag[i]=-1;
  for(int i=1;i<=n;i++){
    if(flag[i]!=-1) continue;
    flag[i]=0;
    queue<int > q;
    q.push(i);//以还未被染过色的i点(还未被搜索过的点)为起始点展开bfs
    while(!q.empty()){
        int head=q.front();
        q.pop();
        for(int j=1;j<=n;j++){
            if(!Map[head][j]) continue;
            if(flag[j]!=-1&&flag[head]==flag[j]) return false;
            else if(flag[j]==-1){flag[j]=!flag[head]; q.push(j);}
        }
    }
  }
  return true;
}

bool match(int x){
   for(int i=1;i<=n;i++){
     if(!vis[i]&&Map[x][i]){
        vis[i]=1;
        if(!used[i]||match(used[i])){
            used[i]=x;
            return true;
        }
     }
   }
   return false;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(Map,0,sizeof(Map));
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            Map[a][b]=Map[b][a]=1;//无向图
        }
        if(bfs_judge()==false) {printf("No\n"); continue;}
        int ans=0;
        memset(used,0,sizeof(used));
        for(int i=1;i<=n;i++){
            memset(vis,0,sizeof(vis));
            if(match(i)) ans++;
        }
        printf("%d\n",ans/2);//每一对匹配被计算了两次
    }
    return 0;
}

以上是关于[图论][二分图判断]The Accomodation of Students的主要内容,如果未能解决你的问题,请参考以下文章

图论二分图的应用(染色法判断二分图,最大匹配,最小点覆盖,最大独立集,最小路径点覆盖,最小路径重复点覆盖)

[UVA1364 | POJ | NC]Knights of the Round Table | Tarjan 求点双 | 二分图 | 综合图论

图论基础——二分图

图论基础——二分图

图论 —— 二分图的判定及最大匹配

The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)