当前位置:网站首页>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 
边栏推荐
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- Sparkcontext: error initializing sparkcontext solution
- Sse/avx instruction set and API of SIMD
- Fluent fluent library encapsulation
- 线性DP AcWing 895. 最长上升子序列
- Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
- Writing method of then part in drools
- Interview questions for software testing - a collection of interview questions for large factories in 2022
- Go learning notes - go based interprocess communication
- 深拷贝 事件总线
猜你喜欢

Deep Copy Event bus

Sparkcontext: error initializing sparkcontext solution

spfa AcWing 851. spfa求最短路

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes

js1day(输入输出语法,数据类型,数据类型转换,var和let区别)

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

Dijkstra AcWing 850. Dijkstra求最短路 II

Redis sentinel mechanism and configuration
随机推荐
C#修饰符
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
哈希表 AcWing 840. 模拟散列表
软件测试面试题-2022年大厂面试题合集
Drools terminates the execution of other rules after executing one rule
Go learning notes - multithreading
About asp Net MVC project in local vs running response time is too long to access, the solution!
Record the range of data that MySQL update will lock
堆 AcWing 838. 堆排序
[I'm a mound pytorch tutorial] learning notes
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
浏览器存储方案
哈希表 AcWing 841. 字符串哈希
VLAN experiment
. Net, C # basic knowledge
Hash table acwing 841 String hash
What data types does redis have and their application scenarios
Redis bloom filter
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Simple use of drools decision table