0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 16:18:46 +00:00

860. 染色法判定二分图

https://www.acwing.com/problem/content/submission/code_detail/36295038/
This commit is contained in:
Baoshuo Ren 2024-08-04 12:58:14 +08:00
parent fa1d75e8f5
commit f110cdee18
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 65 additions and 0 deletions

59
AcWing/860/860.cpp Normal file
View File

@ -0,0 +1,59 @@
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int n, m;
std::vector<int> g[N];
int color[N];
bool dfs(int u, int c) {
color[u] = c;
for (int v : g[u]) {
if (~color[v]) { // 如果已经染色
if (color[v] == c) return false; // 同色,矛盾
} else {
if (!dfs(v, c ^ 1)) return false;
}
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
std::fill(std::begin(color), std::end(color), -1);
cin >> n >> m;
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
// 图可能不连通
for (int i = 1; i <= n; i++) {
if (!~color[i]) { // 如果没有染色
if (!dfs(i, 0)) {
cout << "No" << endl;
exit(0);
}
}
}
cout << "Yes" << endl;
return 0;
}

BIN
AcWing/860/data/17.ans (Stored with Git LFS) Normal file

Binary file not shown.

BIN
AcWing/860/data/17.in (Stored with Git LFS) Normal file

Binary file not shown.