当前位置:网站首页>二分图判定
二分图判定
2022-07-06 14:26:00 【AC__dream】
先来说一下什么是二分图,对于一个图,我们将其顶点集分为两部分A和B,从而使得任意一条边的两个顶点一个在A中一个在B中,而不会出现一条边的两个顶点全部在A中或全部在B中这种情况。
先来说一个定理:一个图是二分图当且仅当这个图中不存在奇数环,也就是不存在一个环的顶点个数是奇数个。
如果利用这个定理来判定一个图是不是二分图的话那么我们就可以使用染色的方法来进行实现,就是每一条边的两个顶点的颜色一定是不相同的,我们按照这个原则来对图进行染色,如果染色成功那么这个图就是一个二分图,否则就不是一个二分图。
给出一道例题及其代码实现:

输入样例:
4 4
1 3
1 4
2 3
2 4输出样例:
Yes代码:
#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]代表第i个节点的颜色
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节点已经染过颜色
{
if(color[j]==c) return false;//一条边的两个顶点颜色相同,矛盾
}
else if(!dfs(j,c^1)) return false;//发现矛盾
}
return true;//所有节点都没有发现矛盾就返回真
}
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);//初始颜色为-1
bool flag=true;
for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
if(!dfs(i,1))//将当前点染成1号颜色
{
flag=false;//出现奇数环就直接退出
break;
}
}
}
if(flag) puts("Yes");
else puts("No");
return 0;
}边栏推荐
- Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
- Codeforces Round #274 (Div. 2) –A Expression
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- [sciter bug] multi line hiding
- How does the uni admin basic framework close the creation of super administrator entries?
- 重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
- Anaconda installs third-party packages
- Assembly and interface technology experiment 5-8259 interrupt experiment
- Unity3d Learning Notes 6 - GPU instantiation (1)
- MongoDB(三)——CRUD
猜你喜欢

GPS从入门到放弃(十七) 、对流层延时
![[线性代数] 1.3 n阶行列式](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[线性代数] 1.3 n阶行列式

二叉(搜索)树的最近公共祖先 ●●

C#實現水晶報錶綁定數據並實現打印4-條形碼

GPS from entry to abandonment (XVII), tropospheric delay

Spatial domain and frequency domain image compression of images
![[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation

硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件

AI 企业多云存储架构实践 | 深势科技分享

Management background --5, sub classification
随机推荐
Classic sql50 questions
Memorabilia of domestic database in June 2022 - ink Sky Wheel
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
[线性代数] 1.3 n阶行列式
Solve project cross domain problems
设置状态栏样式Demo
Oracle Performance Analysis 3: introduction to tkprof
十二、启动流程
2500 common Chinese characters + 130 common Chinese and English characters
GPS从入门到放弃(十九)、精密星历(sp3格式)
Mongodb (III) - CRUD
GPS from getting started to giving up (XVIII), multipath effect
Shortcut keys in the terminal
小满网络模型&http1-http2 &浏览器缓存
C#实现水晶报表绑定数据并实现打印4-条形码
Oracle-控制文件及日志文件的管理
OpenCV300 CMake生成project在项目过程中的问题
Four data streams of grpc
Powerful domestic API management tool
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others