当前位置:网站首页>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 
边栏推荐
- Drools dynamically add, modify, and delete rules
- Leetcode - Sword finger offer 59 - I, 59 - II
- Sse/avx instruction set and API of SIMD
- BOM DOM
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- Introduction to CPU instruction set
- 区间DP AcWing 282. 石子合并
- Wechat official account payment prompt MCH_ ID parameter format error
- JDBC prevent SQL injection problems and solutions [preparedstatement]
- async/await 异步函数
猜你喜欢

Openssh remote enumeration username vulnerability (cve-2018-15473)

应用LNK306GN-TL 转换器、非隔离电源

Deep Copy Event bus

Execute any method of any class through reflection

Record the range of data that MySQL update will lock

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

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

计数类DP AcWing 900. 整数划分
随机推荐
Drools executes the specified rule
浏览器存储方案
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Shutter encapsulated button
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
Heap acwing 839 Simulated reactor
In development, why do you find someone who is paid more than you but doesn't write any code?
线性DP AcWing 895. 最长上升子序列
Sse/avx instruction set and API of SIMD
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
arcgis js 4. Add pictures to x map
[FFH] little bear driver calling process (take calling LED light driver as an example)
获取文件版权信息
Hash table acwing 840 Simulated hash table
Go learning notes - go based interprocess communication
高性能纠删码编码
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
中国交通标志检测数据集
. Net, C # basic knowledge
About asp Net MVC project in local vs running response time is too long to access, the solution!