当前位置:网站首页>Bipartite graph determination
Bipartite graph determination
2022-07-06 22:47:00 【AC__ dream】
First, let's talk about what is a bipartite graph , For a picture , We divide its vertex set into two parts A and B, Thus, one of the two vertices of any edge is A One of them is in B in , Instead of having two vertices of an edge all in A In or all of B In this case .
Let's start with a theorem : A graph is a bipartite graph if and only if there are no odd rings in this graph , That is, there is no ring with an odd number of vertices .
If we use this theorem to determine whether a graph is a bipartite graph, then we can use the method of coloring to realize , The color of the two vertices of each edge must be different , We dye the graph according to this principle , If the coloring is successful, then this graph is a bipartite graph , Otherwise, it is not a bipartite graph .
Give an example and its code implementation :

sample input :
4 4
1 3
1 4
2 3
2 4sample output :
YesCode :
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
const int N=2e5+10;
int h[N],e[N],ne[N],color[N],idx;
//color[i] On behalf of the i Color of nodes
void add(int x,int y)
{
e[idx]=y;
ne[idx]=h[x];
h[x]=idx++;
}
bool dfs(int x,int c)
{
color[x]=c;
for(int i=h[x];i!=-1;i=ne[i])
{
int j=e[i];
if(color[j]!=-1)//j Nodes have been dyed
{
if(color[j]==c) return false;// Two vertices of an edge have the same color , contradiction
}
else if(!dfs(j,c^1)) return false;// Find contradictions
}
return true;// All nodes return true without finding contradictions
}
int main()
{
int n,m;
cin>>n>>m;
memset(h,-1,sizeof h);
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
memset(color,-1,sizeof color);// The initial color is -1
bool flag=true;
for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
if(!dfs(i,1))// Dye the current dot into 1 Number color
{
flag=false;// Exit directly when there is an odd ring
break;
}
}
}
if(flag) puts("Yes");
else puts("No");
return 0;
}边栏推荐
- volatile关键字
- Puppeter connects to the existing Chrome browser
- POJ 1094 sorting it all out
- Pit encountered by handwritten ABA
- The ceiling of MySQL tutorial. Collect it and take your time
- 【LeetCode】19、 删除链表的倒数第 N 个结点
- MySQL教程的天花板,收藏好,慢慢看
- CocosCreator+TypeScripts自己写一个对象池
- Sword finger offer question brushing record 1
- Aardio - construct a multi button component with customplus library +plus
猜你喜欢

Mysql 身份认证绕过漏洞(CVE-2012-2122)

专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统

Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)

Cocoscreator+typescripts write an object pool by themselves

自定义 swap 函数

European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold

AdaViT——自适应选择计算结构的动态网络

C three ways to realize socket data reception

Signed and unsigned keywords

Rust knowledge mind map XMIND
随机推荐
Some suggestions for foreign lead2022 in the second half of the year
OpenSSL: a full-featured toolkit for TLS and SSL protocols, and a general encryption library
使用云服务器搭建代理
Windows Auzre 微软的云计算产品的后台操作界面
CUDA exploration
(18) LCD1602 experiment
枚举与#define 宏的区别
Mysql database basic operations DML
The difference between enumeration and define macro
hdu 5077 NAND(暴力打表)
three. JS gorgeous bubble effect
UVa 11732 – strcmp() Anyone?
Aardio - construct a multi button component with customplus library +plus
MySQL教程的天花板,收藏好,慢慢看
Detailed explanation of ThreadLocal
Cocoscreator+typescripts write an object pool by themselves
Custom swap function
视图(view)
空结构体多大?
volatile关键字