判断一个无向图是否为二分图

怎样判断一个图是否为二分图?

无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶数。

如果需要分出哪些顶点是一组,很简单,用染色法,即从其中一个顶点开始,将跟它邻接的点染成与其不同的颜色,如果邻接的点有相同颜色的,则说明不是二分图,用 bfs 或 dfs 遍历即可。

#include <queue>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 510;
int col[N], g[N][N];

//0为白色,1为黑色 
bool bfs(int s, int n) {
    queue<int> p;
    p.push(s);
    col[s] = 1;
    while(!p.empty()) {
        int from = p.front();
        p.pop();
        for(int i = 1; i <= n; i++) {
            if(g[from][i] && col[i] == -1) {
                p.push(i);
                col[i] = !col[from];//染成不同的颜色 
            }
            if(g[from][i] && col[from] == col[i])//颜色有相同,则不是二分图 
                return false;
        }
    }
    return true;     
}

int main() {
    int n, m, a, b, i;
    memset(col, -1, sizeof(col));
    cin >> n >> m;
    for(i = 0; i < m; i++) {
        cin >> a >> b;
        g[a][b] = g[b][a] = 1; 
    }
    bool flag = false;
    for(i = 1; i <= n; i++)
        if(col[i] == -1 && !bfs(i, n)) {//遍历各个连通分支 
            flag = true;
            break;  
        }
    if(flag)
        cout << "NO" <<endl;    
    else
        cout << "YES" <<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/scarecrow-blog/p/3711351.html