-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path785.cpp
More file actions
28 lines (28 loc) · 810 Bytes
/
785.cpp
File metadata and controls
28 lines (28 loc) · 810 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
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> check(n,-1);
queue<int> q;
bool flag = true;
for(int u= 0; u<n; u++){
if(check[u]==-1){
q.push(u);
check[u] = 0;
while(!q.empty()){
auto curr = q.front();q.pop();
for(auto v: graph[curr]){
if(check[v]==-1){
check[v] = check[curr] ^ 1;
q.push(v);
}
else{
flag &= check[curr]!=check[v];
}
}
}
}
}
return flag;
}
};