当前位置:网站首页>二分图判定
二分图判定
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;
}
边栏推荐
- 【sciter】: 基于 sciter 封装通知栏组件
- AI 企业多云存储架构实践 | 深势科技分享
- 【10点公开课】:视频质量评价基础与实践
- Write a rotation verification code annotation gadget with aardio
- Management background --4, delete classification
- Lora sync word settings
- Codeforces Round #274 (Div. 2) –A Expression
- GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
- 【MySQL】Online DDL详解
- 12、 Start process
猜你喜欢
图像的spatial domain 和 frequency domain 图像压缩
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
GPS from entry to abandonment (XVII), tropospheric delay
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
[leetcode daily clock in] 1020 Number of enclaves
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
C#實現水晶報錶綁定數據並實現打印4-條形碼
GPS from getting started to giving up (12), Doppler constant speed
Classic sql50 questions
随机推荐
Unity3d Learning Notes 6 - GPU instantiation (1)
NetXpert XG2帮您解决“布线安装与维护”难题
ZABBIX proxy server and ZABBIX SNMP monitoring
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
Force buckle 575 Divide candy
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
功能强大的国产Api管理工具
QT | UDP broadcast communication, simple use case
GPS from getting started to giving up (XV), DCB differential code deviation
Solve project cross domain problems
HDR image reconstruction from a single exposure using deep CNNs阅读札记
Learn the principle of database kernel from Oracle log parsing
[sciter]: encapsulate the notification bar component based on sciter
Chapter 4: talk about class loader again
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
GPS从入门到放弃(二十)、天线偏移
Write a rotation verification code annotation gadget with aardio
What is the difference between animators and animators- What is the difference between an Animator and an Animation?
AI 企业多云存储架构实践 | 深势科技分享
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function