当前位置:网站首页>染色法判定二分图 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Deep copy event bus
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- [ybtoj advanced training guidance] cross the river [BFS]
- arcgis js 4. Add pictures to x map
- Leetcode14 longest public prefix
- [ybtoj advanced training guidance] judgment overflow [error]
- (C language) octal conversion decimal
- Maximum profit of jz63 shares
- Post request body content cannot be retrieved repeatedly
- Heap (priority queue)
猜你喜欢
![[ybtoj advanced training guide] similar string [string] [simulation]](/img/eb/acfefc7f85018fe9365d13502e2b0a.jpg)
[ybtoj advanced training guide] similar string [string] [simulation]

Record the range of data that MySQL update will lock

堆(优先级队列)

Map和Set

BOM DOM

(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately

What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT

包管理工具
随机推荐
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Docker compose configuration mysql, redis, mongodb
CDA data analysis -- Introduction and use of aarrr growth model
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
[I'm a mound pytorch tutorial] learning notes
FBX import under ue4/ue5 runtime
MySQL与PostgreSQL抓取慢sql的方法
ThreadLocal的简单理解
堆(優先級隊列)
Leetcode739 daily temperature
堆(优先级队列)
SparkContext: Error initializing SparkContext解决方法
Is the neural network (pinn) with embedded physical knowledge a pit?
Bom Dom
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
AI中台技术调研
Day12 control flow if switch while do While guessing numbers game
Differences between nodes and sharding in ES cluster
深拷貝 事件總線
Performance tuning project case