[Graph Theory][Bipartite Graph Judgment]

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
 
Ideas: Judging whether it is a bipartite graph: In an undirected graph G, if there is an odd loop (the number of nodes in the loop is an odd number), it is not a bipartite graph. Otherwise it is a bipartite graph.
           The dyeing method judges the parity of the loop: dye two adjacent points in black and white, and if the two adjacent points have the same color, there is an odd loop. That is, a non-bipartite graph.
AC code:
#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(){ // Dyeing method to judge whether it is a bipartite graph 
  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); // Expand bfs from the point i that has not been dyed (the point that has not been searched) as the starting point 
    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;
}

intmain ()
{
    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 ); // Each pair of matches is counted twice 
    }
     return  0 ;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325804172&siteId=291194637