-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbipartite.cpp
More file actions
32 lines (26 loc) · 777 Bytes
/
bipartite.cpp
File metadata and controls
32 lines (26 loc) · 777 Bytes
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
class Solution {
public:
bool isBipartite(vector<vector<int>>& gr) {
int n = gr.size();
vector<int> colour(n, 0);
for(int node = 0; node < n; node++){
if(colour[node] != 0) continue;
queue<int> q;
q.push(node);
colour[node] = 1;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int ne : gr[cur]){
if(colour[ne] == 0){
colour[ne] = -colour[cur];
q.push(ne);
}else if(colour[ne] != -colour[cur]){
return false;
}
}
}
}
return true;
}
};