-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_bipartite.cpp
54 lines (45 loc) · 1.05 KB
/
check_bipartite.cpp
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
#include <bits/stdc++.h>
using namespace std;
/*
https://youtu.be/-vu34sct1g8
*/
class Solution
{
private:
bool check(int start, vector<vector<int>> graph, vector<int> &color)
{
queue<int> q;
q.push(start);
color[start] = 0;
while (!q.empty())
{
int node = q.front();
q.pop();
for (auto it : graph[node])
{
if (color[it] == -1)
{
color[it] = !color[node];
q.push(it);
}
else if (color[it] == color[node])
return false;
}
}
return true;
}
public:
bool isBipartite(vector<vector<int>> &graph)
{
int V = graph.size();
// convert matrix to list
vector<int> color(V, -1);
for (int i = 0; i < V; i++)
if(color[i] == -1)
{
if (check(i, graph, color) == false)
return false;
}
return true;
}
};