AcWing 860. 染色法判定二分图
原题链接
简单
作者:
Value
,
2020-05-27 09:17:54
,
所有人可见
,
阅读 334
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1E5 + 10;
int head[N], value[N*2], nxt[N*2];
int idx;
int n, m;
void add(int a, int b){
value[idx] = b;
nxt[idx] = head[a];
head[a] = idx ++ ;
}
void read(){
memset(head, -1, sizeof head);
cin >> n >> m;
int a, b;
while(m -- ){
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
}
int color[N];
bool dfs(int u, int c){
color[u] = c;
for(int i = head[u]; i != -1; i = nxt[i]){
int p = value[i];
if(!color[p]){
if(!dfs(p, 3 - c)) return false;
}else{
if(color[p] == c) return false;
}
}
return true;
}
int main(){
read();
bool res = true;
for(int i = 1; i <= n; i ++ ){
if(!color[i]){
if(!dfs(i, 1)){
res = false;
break;
}
}
}
if(res) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}