当前位置:网站首页>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
1and2Distinguish between different colors , use0Indicates not stained . - Traverse all points , Each time, the undyed points are searched for depth first , Default dyeing
1perhaps2 - 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;
}
边栏推荐
- Detailed explanation of balanced binary tree is easy to understand
- Database course design: college educational administration management system (including code)
- [untitled]
- Usage differences between isempty and isblank
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- 基本Dos命令
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- Database table splitting strategy
- MySQL error warning: a long semaphore wait
猜你喜欢

Office prompts that your license is not genuine pop-up box solution

基本Dos命令
![[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
![[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity](/img/9d/7284c1399964d3fb48886f12e4941c.jpg)
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity

使用rtknavi进行RT-PPP测试

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现

FairyGUI简单背包的制作

闇の連鎖(LCA+树上差分)

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

On March 15, the official version of go 1.18 was released to learn about the latest features and usage
随机推荐
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
闇の連鎖(LCA+树上差分)
记录:初次cmd启动MySQL拒接访问之解决
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
[Chongqing Guangdong education] Shandong University College Physics reference materials
服务未正常关闭导致端口被占用
Liste des boucles de l'interface graphique de défaillance
Matlab读取GNSS 观测值o文件代码示例
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
Excel导入,导出功能实现
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
Usage differences between isempty and isblank
Game 280 weekly
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
FairyGUI增益BUFF數值改變的顯示
微信小程序开发心得
[算法] 劍指offer2 golang 面試題2:二進制加法
VLSM variable length subnet mask partition tips