当前位置:网站首页>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 
边栏推荐
- Deep copy event bus
- 8A 同步降压稳压器 TPS568230RJER_规格信息
- IPhone 6 plus is listed in Apple's "retro products" list
- Fluent fluent library encapsulation
- JSON序列化 与 解析
- Sparkcontext: error initializing sparkcontext solution
- Calculate the maximum path sum of binary tree
- VLAN experiment
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
- 趣味 面试题
猜你喜欢
![JDBC prevent SQL injection problems and solutions [preparedstatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC prevent SQL injection problems and solutions [preparedstatement]

Redis sentinel mechanism and configuration

趣味 面试题

js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async

Find the common ancestor of any two numbers in a binary tree

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

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

Simple use of drools decision table

Heap acwing 838 Heap sort

哈希表 AcWing 841. 字符串哈希
随机推荐
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
. Net, C # basic knowledge
Sparkcontext: error initializing sparkcontext solution
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
H5 to app
Simple understanding of ThreadLocal
Input box assembly of the shutter package
VLAN experiment
包管理工具
分布式机器学习框架与高维实时推荐系统
哈希表 AcWing 840. 模拟散列表
Drools executes the specified rule
线性DP AcWing 899. 编辑距离
Simple use of drools decision table
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
染色法判定二分图 AcWing 860. 染色法判定二分图
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
防抖 节流
Adding database driver to sqoop of cdh6
Sweetheart leader: Wang Xinling