当前位置:网站首页>染色法判定二分图 AcWing 860. 染色法判定二分图
染色法判定二分图 AcWing 860. 染色法判定二分图
2022-07-02 09:43:00 【T_Y_F666】
染色法判定二分图 AcWing 860. 染色法判定二分图
原题链接
算法标签
二分图判定 染色法
思路
二分图就是可以把所有点划分到两边集合中去,使得所有的边在两个集合外且在两个集合之间,集合内部没有边
二分图充要条件
充分性
可以通过构造, 将图中某点颜色染为颜色1, 则于其相邻点染为颜色2证明:
反证法, 若存在奇数环, 一定存在矛盾
因次, 对图中每点进行染色, 判断是否有矛盾。
若存在矛盾则不是二分图, 否则表明可以染色, 是二分图。
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100005, INF = 0x3f3f3f3f;
// 存储无向图 需要同时存储两条边 因此需要将数组e[], ne[]开辟两倍空间
int e[N*2], ne[N*2], h[N], idx;
int color[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
void add(int a, int b){
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]){
// 相邻节点dfs后颜色相同
if(!dfs(j, 3-c)){
return false;
}
}else if(color[j]==c){// 相邻节点颜色相同
return false;
}
}
return true;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(), m=read();
memset(h, -1, sizeof h);
while(m--){
int a=read(), b=read();
add(a, b), add(b, a);
}
bool flag=true;
rep(i, 1, n+1){
if(!color[i]){
if(!dfs(i, 1)){
flag=false;
break;
}
}
}
if(flag){
puts("Yes");
}else{
puts("No");
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Shuttle encapsulated AppBar
- [old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
- 倍增 LCA(最近公共祖先)
- 计算二叉树的最大路径和
- lombok常用注解
- mysql数据库基础
- Jenkins user rights management
- Input box assembly of the shutter package
- 包管理工具
- SSH automatically disconnects (pretends to be dead) after a period of no operation
猜你喜欢
Enhance network security of kubernetes with cilium
【工控老马】西门子PLC Siemens PLC TCP协议详解
[ybtoj advanced training guide] similar string [string] [simulation]
AI mid stage technology research
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
ThreadLocal的简单理解
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
arcgis js 4.x 地图中加入图片
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
JSON序列化 与 解析
随机推荐
Enhance network security of kubernetes with cilium
Anti shake throttle
WSL 2 will not be installed yet? It's enough to read this article
Performance tuning project case
CPU指令集介绍
怎样写一篇赏心悦目的英文数学论文
Input box assembly of the shutter package
CDA数据分析——Excel数据处理的常见知识点归纳
[I'm a mound pytorch tutorial] learning notes
Writing method of then part in drools
Sparkcontext: error initializing sparkcontext solution
Leetcode209 subarray with the smallest length
How to write a pleasing English mathematical paper
lombok常用注解
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Openssh remote enumeration username vulnerability (cve-2018-15473)
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
IPhone 6 plus is listed in Apple's "retro products" list
[FFH] little bear driver calling process (take calling LED light driver as an example)
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了