当前位置:网站首页>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;
}边栏推荐
- Extern keyword
- NPM cannot install sharp
- Improving Multimodal Accuracy Through Modality Pre-training and Attention
- hdu 5077 NAND(暴力打表)
- QT signal and slot
- 项目复盘模板
- 2022-07-05 use TPCC to conduct sub query test on stonedb
- Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
- Plafond du tutoriel MySQL, bien collecté, regardez lentement
- Inno setup packaging and signing Guide
猜你喜欢

Leetcode exercise - Sword finger offer 26 Substructure of tree

Clip +json parsing converts the sound in the video into text

Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system

Improving Multimodal Accuracy Through Modality Pre-training and Attention

Sword finger offer question brushing record 1

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

Should novice programmers memorize code?

UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop

Aardio - 通过变量名将变量值整合到一串文本中

signed、unsigned关键字
随机推荐
Sword finger offer question brushing record 1
#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)
Mysql database basic operations DML
Aardio - 利用customPlus库+plus构造一个多按钮组件
Cloud native technology container knowledge points
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
MySQL数据库基本操作-DML
「小程序容器技术」,是噱头还是新风口?
memcached
View
MySQL教程的天花板,收藏好,慢慢看
volatile关键字
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
(18) LCD1602 experiment
如何用程序确认当前系统的存储模式?
What are the specific steps and schedule of IELTS speaking?
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
2022-07-05 stonedb sub query processing parsing time analysis
Slide the uniapp to a certain height and fix an element to the top effect demo (organize)