当前位置:网站首页>Chromatic judgement bipartite graph
Chromatic judgement bipartite graph
2022-07-06 12:56:00 【Classmate Chen_】
One 、 What is a bipartite graph ( Bipartite graph )?
Definition : Bipartite graphs are also called bipartite graphs , It's a special model in graph theory . set up G=(V,E) It's an undirected graph , If the vertex V It can be divided into two disjoint subsets (A,B), And every edge in the graph (i,j) The two vertices associated i and j They belong to these two different vertex sets (i in A,j in B), It's called a picture G For a bipartite graph .
In other words : All points of a graph can be divided into two disjoint subsets , And the two points attached to each edge in the graph belong to these two disjoint subsets , The vertices in two subsets are not adjacent .
The following three graphs are bipartite graphs :
Two 、 How can we distinguish a bipartite graph ?
By definition , Distinguishing a bipartite graph is whether all points in the graph can be divided into two independent point sets .
To prove it , If a graph is a bipartite graph , Then there must not be an odd loop in the graph .
Because if there is a circuit in a graph and the length of this circuit is odd , Then the points in this circuit should also be odd . According to the previous definition , The two points attached to each edge belong to these two disjoint subsets , However, the number of points is odd , Therefore, it must not be divided into two disjoint subsets .
For example, the following figure is not a bipartite graph , We can find whether the dots in the box are white or red , Will make two points attached to an edge in a subset ( Same color ).
Here we use the coloring method to judge the bipartite graph ( Depth first search version )
subject
Given a n A little bit m Undirected graph of strip and edge , There may be double edges and self rings in the graph .
Please judge whether this graph is bipartite .
Input format
The first line contains two integers n and m.
Next m That's ok , Each line contains two integers u and v, Indication point u Sum point v There is an edge between .
Output format
If a given graph is a bipartite graph , The output Yes
, Otherwise output No
.
Data range
1≤n,m≤10^5
sample input :
4 4
1 3
1 4
2 3
2 4
sample output :
Yes
This is a very basic introductory question , The title is to give us a picture , Let's judge whether it is a bipartite graph .
Ideas :
- Dyeing can use
1
and2
Distinguish between different colors , use0
Indicates not stained . - Traverse all points , Each time, the undyed points are searched for depth first , Default dyeing
1
perhaps2
- Here, if we cannot judge whether a certain point is dyed successfully, it means that the graph is a bipartite graph , To judge whether a certain point fails to stain ; Coloring failure is not a bipartite graph , Otherwise, it is a bipartite graph .
AC Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+10,M=2*1e5+10; // Undirected graph , Number of edges *2
int h[N],e[M],ne[M],idx;
int color[N];
int n,m;
void add(int a,int b){
// Adjacency list
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
bool dfs(int u,int c){
color[u]=c;
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(!color[j]){
if(!dfs(j,3-c)) return false; // If at present j Nodes are not stained , Will judge j Will there be contradictions when nodes are dyed with another color , In case of contradiction, return false( Is not a bipartite graph )
}
else if(color[j]==c) return false; // If the adjacent nodes have the same color , It's not a bipartite graph
}
return true; // If there is no dyeing failure, it will succeed
}
int main(){
int a,b;
memset(h,-1,sizeof h);
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&a,&b);
add(a,b); // Undirected graph
add(b,a);
}
bool flag=true;
for(int i=1;i<=n;i++){
if(!color[i]){
if(!dfs(i,1)){
flag=false;
break;
}
}
}
if(flag)puts("Yes");
else puts("No");
return 0;
}
边栏推荐
- Guided package method in idea
- [untitled]
- GPS高程拟合抗差中误差的求取代码实现
- Database table splitting strategy
- 记录:动态Web项目servlet访问数据库404错误之解决
- [Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
- Unity3d makes the registration login interface and realizes the scene jump
- Implementation of Excel import and export functions
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
猜你喜欢
Unity3d makes the registration login interface and realizes the scene jump
记录:初次cmd启动MySQL拒接访问之解决
(core focus of software engineering review) Chapter V detailed design exercises
Guided package method in idea
The port is occupied because the service is not shut down normally
Liste des boucles de l'interface graphique de défaillance
Naive Bayesian theory derivation
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
使用rtknavi进行RT-PPP测试
Mysql database index
随机推荐
In 2020, the average salary of IT industry exceeded 170000, ranking first
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
FairyGUI增益BUFF数值改变的显示
记录:动态Web项目servlet访问数据库404错误之解决
Office prompts that your license is not genuine pop-up box solution
【干货】提升RTK模糊度固定率的建议之周跳探测
idea中好用的快捷键
基于rtklib源码进行片上移植的思路分享
KF UD分解之伪代码实现进阶篇【2】
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
异常:IOException:Stream Closed
Code example of MATLAB reading GNSS observation value o file
【GNSS】抗差估计(稳健估计)原理及程序实现
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
MySQL performance tuning - dirty page refresh
堆排序【手写小根堆】
Theoretical derivation of support vector machine
FairyGUI条子家族(滚动条,滑动条,进度条)
[algorithm] sword finger offer2 golang interview question 1: integer division
Compile GDAL source code with nmake (win10, vs2022)