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