当前位置:网站首页>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 
边栏推荐
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- 2.7 binary tree, post order traversal - [FBI tree]
- About the loading of layer web spring layer components, the position of the layer is centered
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- Heap acwing 839 Simulated reactor
- 趣味 面试题
- js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
- 2.6 using recursion and stack - [tower of Hanoi problem]
- 百款拿来就能用的网页特效,不来看看吗?
猜你喜欢

Redis sentinel mechanism and configuration

3 A VTT端接 稳压器 NCP51200MNTXG资料

Deep copy event bus

中国交通标志检测数据集

Heap acwing 839 Simulated reactor

spfa AcWing 851. SPFA finding the shortest path

区间DP AcWing 282. 石子合并

线性DP AcWing 899. 编辑距离

计数类DP AcWing 900. 整数划分

Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
随机推荐
软件测试面试题-2022年大厂面试题合集
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
Floyd AcWing 854. Floyd求最短路
[I'm a mound pytorch tutorial] learning notes
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
. Net wechat message template push
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
包管理工具
Heap acwing 839 Simulated reactor
Oracle从入门到精通(第4版)
一些突然迸发出的程序思想(模块化处理)
Drools terminates the execution of other rules after executing one rule
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
spfa AcWing 852. spfa判断负环
哈希表 AcWing 840. 模拟散列表
Performance tuning project case
Record the range of data that MySQL update will lock
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
Day12 control flow if switch while do While guessing numbers game