mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 17:08:48 +00:00
860. 染色法判定二分图
https://www.acwing.com/problem/content/submission/code_detail/36295038/
This commit is contained in:
parent
fa1d75e8f5
commit
f110cdee18
59
AcWing/860/860.cpp
Normal file
59
AcWing/860/860.cpp
Normal 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
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
BIN
AcWing/860/data/17.in
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user