当前位置:网站首页>染色法判定二分图 AcWing 860. 染色法判定二分图
染色法判定二分图 AcWing 860. 染色法判定二分图
2022-07-02 09:43:00 【T_Y_F666】
染色法判定二分图 AcWing 860. 染色法判定二分图
原题链接
算法标签
二分图判定 染色法
思路
二分图就是可以把所有点划分到两边集合中去,使得所有的边在两个集合外且在两个集合之间,集合内部没有边
二分图充要条件
充分性
可以通过构造, 将图中某点颜色染为颜色1, 则于其相邻点染为颜色2
证明:
反证法, 若存在奇数环, 一定存在矛盾
因次, 对图中每点进行染色, 判断是否有矛盾。
若存在矛盾则不是二分图, 否则表明可以染色, 是二分图。
代码
#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;
// 存储无向图 需要同时存储两条边 因此需要将数组e[], ne[]开辟两倍空间
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]){
// 相邻节点dfs后颜色相同
if(!dfs(j, 3-c)){
return false;
}
}else if(color[j]==c){// 相邻节点颜色相同
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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- LeetCode—<动态规划专项>剑指 Offer 19、49、60
- Simple use of drools decision table
- Docker compose configuration mysql, redis, mongodb
- 倍增 LCA(最近公共祖先)
- Post request body content cannot be retrieved repeatedly
- 单指令多数据SIMD的SSE/AVX指令集和API
- Intel internal instructions - AVX and avx2 learning notes
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- Record the range of data that MySQL update will lock
- arcgis js 4.x 地图中加入图片
猜你喜欢

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

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

Differences between nodes and sharding in ES cluster

Sparkcontext: error initializing sparkcontext solution
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

Deep Copy Event bus

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

drools决策表的简单使用

BOM DOM

BOM DOM
随机推荐
Drools executes string rules or executes a rule file
drools执行指定的规则
Bom Dom
CDA数据分析——Excel数据处理的常见知识点归纳
Sparkcontext: error initializing sparkcontext solution
Find the common ancestor of any two numbers in a binary tree
Multiply LCA (nearest common ancestor)
Sse/avx instruction set and API of SIMD
Leetcode739 daily temperature
China traffic sign detection data set
Leetcode922 sort array by parity II
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
drools执行String规则或执行某个规则文件
IPhone 6 plus is listed in Apple's "retro products" list
Go学习笔记—多线程
Introduction to CPU instruction set
Input box assembly of the shutter package
倍增 LCA(最近公共祖先)