blob: 71ed2c22bcffd2d6efd72e7c0f6f7da61ed3d9e0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//#include <gtest/gtest.h>
#include <iostream>
#include <vector>
#include <queue>
using std::vector;
using std::queue;
bool bfsBipartiteCheck(vector<vector<int>> &adj, int s) {
if (adj[s].size() == 0)
return true;
vector<int> dist(adj.size(), -1);
dist[s] = 0;
queue<int> vertexQ;
vertexQ.push(s);
while (!vertexQ.empty()) {
int u = vertexQ.front();
vertexQ.pop();
for (int i : adj[u]) {
if (dist[i] == -1) {
vertexQ.push(i);
if (dist[u] == 0)
dist[i] = 1;
if (dist[u] == 1)
dist[i] = 0;
} else if (dist[i] == dist[u])
return false;
}
}
return true;
}
int bipartite(vector<vector<int> > &adj) {
bool bipartiteCheckNodes = bfsBipartiteCheck(adj, 0);
if (bipartiteCheckNodes)
return 1;
else
return 0;
}
int main() {
int n, m;
std::cin >> n >> m;
vector<vector<int> > adj(n, vector<int>());
for (int i = 0; i < m; i++) {
int x, y;
std::cin >> x >> y;
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
std::cout << bipartite(adj);
}
//TEST(Acyclicity, ac1) {
// vector<vector<int>> adj = { { 1 }, { 2 }, { 0 }, { 0 } };
// ASSERT_EQ(acyclic(adj), 1);
//}
|