Algorithm-Judging the bipartite graph

  • Notes:
  • Algorithm core : If a graph is a bipartite graph, its necessary and sufficient condition is that all loops are even, that is, each loop can be colored with two colors, that is, the graph can be colored with two colors, and you can directly use dfs \small dfsd f s dyeing judges whether it can be dyed with two colors.
  • Demonstration diagram :


    Insert picture description here
  • Code
#include<vector>
#include<iostream>
using namespace std;

const int maxn = 1e5 + 10;

//用1和-1染色,染色的数组
int col[maxn];
//用向量建图
vector<int>vt[maxn];

//判断u结点是否可以用c染色
//参数含义  --  u : 结点  c : 颜色
bool judge(int u, int c) {
    
    
	//如果u结点未染色,将u结点染为c
	if (col[u] == 0) col[u] = c;
	//遍历与u结点相连的结点
	for (int i = 0; i < vt[u].size(); i++) {
    
    
		//保存u相连的结点,方便书写
		int v = vt[u][i];
		//如果u,v颜色相同,说明不可以用两种颜色染色
		if (col[v] == col[u]) return 0;
		//如果v没有染色且v不能用-c染色,说明不可以用两种颜色染色
		if (col[v] == 0 && !judge(v, -c)) return 0;
	}
	return 1;
}
int main() {
    
    

	//v : 点数  e : 边数 
	int v, e;
	cin >> v >> e;

	while (e--) {
    
    
		int u, v;
		cin >> u >> v;
		//建无向图
		vt[u].push_back(v);
		//有向图去掉这句话
		vt[v].push_back(u);
	}

	//多组输入的话初始化
	//memset(col, 0, sizeof col);
	for (int i = 1; i <= v; i++) {
    
    
		//如果未染色,判断这个点可不可以用1染色
		if (col[i] == 0) {
    
    
			if (judge(i, 1) == 0) {
    
    
				cout << "No" << endl;
				return 0;
			}
		}
	}

	cout << "Yes" << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45739057/article/details/107194722