当前位置:网站首页>The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
2022-07-02 12:43:00 【T_ Y_ F666】
Determine bipartite graph by coloring AcWing 860. Determine bipartite graph by coloring
Original link
AcWing 860. Determine bipartite graph by coloring
Algorithm tags
Bipartite graph decision Dyeing method
Ideas
A bipartite graph can divide all points into sets on both sides , Make all edges outside and between two sets , There are no edges inside the set
Necessary and sufficient conditions for bipartite graph 
adequacy
It can be constructed by , Dye the color of a point in the figure into color 1, Then the adjacent dots are dyed as color 2
prove :
Reduction to absurdity , If there is an odd ring , There must be contradictions 
Cause , Dye every point in the graph , Judge whether there are contradictions .
If there are contradictions, it is not a bipartite graph , Otherwise, it indicates that it can be dyed , It's a bipartite graph .
Code
#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;
// Store undirected graph You need to store both sides at the same time Therefore, you need to set the array e[], ne[] Open up twice the space
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]){
// Adjacent nodes dfs Same color after
if(!dfs(j, 3-c)){
return false;
}
}else if(color[j]==c){// Adjacent nodes have the same color
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 百款拿来就能用的网页特效,不来看看吗?
- Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
- Embedded Software Engineer career planning
- 3 A VTT端接 稳压器 NCP51200MNTXG资料
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- 哈希表 AcWing 841. 字符串哈希
- Sweetheart leader: Wang Xinling
- 2.7 binary tree, post order traversal - [FBI tree]
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
- Heap acwing 838 Heap sort
猜你喜欢
![2.6 using recursion and stack - [tower of Hanoi problem]](/img/fc/45038170dafd104691c93716b103cf.jpg)
2.6 using recursion and stack - [tower of Hanoi problem]

js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)

模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计

Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately

BOM DOM

Dijkstra AcWing 850. Dijkstra finding the shortest circuit II

BOM DOM

堆 AcWing 838. 堆排序

Hash table acwing 841 String hash

趣味 面试题
随机推荐
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
JDBC prevent SQL injection problems and solutions [preparedstatement]
spfa AcWing 851. spfa求最短路
Is the neural network (pinn) with embedded physical knowledge a pit?
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
一些突然迸发出的程序思想(模块化处理)
Use sqoop to export ads layer data to MySQL
软件测试面试题-2022年大厂面试题合集
Introduction to CPU instruction set
Docker compose configuration mysql, redis, mongodb
Intel 内部指令 --- AVX和AVX2学习笔记
Shutter encapsulated button
线性DP AcWing 902. 最短编辑距离
std::vector批量导入快速去重方法
应用LNK306GN-TL 转换器、非隔离电源
中国交通标志检测数据集
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
Day12 control flow if switch while do While guessing numbers game