-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0785.cpp
More file actions
25 lines (24 loc) · 701 Bytes
/
Copy path0785.cpp
File metadata and controls
25 lines (24 loc) · 701 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
class Solution {
public:
bool isBipartite(vector<vector<int>> &graph) {
int n = graph.size();
vector<int> color(n, 0);
for (int i = 0; i < n; i++) {
if (color[i]) continue;
stack<int> st;
st.push(i), color[i] = 1;
while (!st.empty()) {
int root = st.top();
st.pop();
for (int c : graph[root]) {
if (color[root] == color[c]) return false;
if (!color[c]) {
st.push(c);
color[c] = -color[root];
}
}
}
}
return true;
}
};