Files
python/TangDou/AcWing/BipartiteGraph/860_bfs.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

58 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = N << 1;
typedef pair<int, int> PII;
int n, m;
int color[N];
// 邻接表
int e[M], h[N], idx, ne[M];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool bfs(int x) {
// 假设 1:黑,2:白,这样方便理解一些
color[x] = 1;
queue<PII> q;
q.push({x, 1});
while (q.size()) {
PII t = q.front();
q.pop();
int u = t.first, c = t.second;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!color[v]) {
color[v] = 3 - c;
q.push({v, 3 - c});
} else if (color[v] == c) // 发现冲突
return 0;
}
}
return 1;
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
int a, b;
while (m--) {
cin >> a >> b;
add(a, b), add(b, a);
}
int flag = 1;
for (int i = 1; i <= n; i++) {
if (!color[i]) {
if (!bfs(i)) {
flag = 0;
break;
}
}
}
if (flag)
puts("Yes");
else
puts("No");
return 0;
}