当前位置:网站首页>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;
}
边栏推荐
- In 2020, the average salary of IT industry exceeded 170000, ranking first
- 第一人称视角的角色移动
- Special palindromes of daily practice of Blue Bridge Cup
- rtklib单点定位spp使用抗差估计遇到的问题及解决
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- 记录:初次cmd启动MySQL拒接访问之解决
- Itext 7 生成PDF总结
- Knowledge system of digital IT practitioners | software development methods -- agile
- FairyGUI循环列表
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
猜你喜欢

Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)

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

【无标题】

Teach you to release a DeNO module hand in hand

Fairygui gain buff value change display

KF UD分解之UD分解基础篇【1】
![[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k](/img/65/fc3fb5a217a3b44f506b695af53e2c.png)
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
![[算法] 剑指offer2 golang 面试题4:只出现一次的数字](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
![[algorithm] sword finger offer2 golang interview question 1: integer division](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[algorithm] sword finger offer2 golang interview question 1: integer division

Programming homework: educational administration management system (C language)
随机推荐
Devops' future: six trends in 2022 and beyond
第一人称视角的角色移动
基本Dos命令
记录:初次cmd启动MySQL拒接访问之解决
[algorithm] sword finger offer2 golang interview question 2: binary addition
Meanings and differences of PV, UV, IP, VV, CV
Lean product development - Lean Software Development & lean product development
FairyGUI摇杆
编辑距离(多源BFS)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Database table splitting strategy
Problems and solutions of robust estimation in rtklib single point location spp
平衡二叉树详解 通俗易懂
FairyGUI循環列錶
C programming exercise
Unity3d makes the registration login interface and realizes the scene jump
idea问题记录
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
KF UD分解之伪代码实现进阶篇【2】
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire