-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path785.cpp
27 lines (27 loc) · 841 Bytes
/
785.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
class Solution {
public:
bool isBipartite(vector<vector<int>> &graph) {
int n = graph.size();
vector<int> part(n, -1);
for (int i = 0; i < n; ++i) {
if (part[i] == -1) {
queue<int> que;
que.push(i);
part[i] = 0;
int size = 1;
while (!que.empty()) {
auto root = que.front();
que.pop();
for (auto &node:graph[root]) {
if (part[node] == -1) {
part[node] = part[root] ^ 1;
que.push(node);
} else if (part[node] == part[root])
return false;
}
}
}
}
return true;
}
};