leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0785.cpp (701B)
0 class Solution {
1 public:
2 bool isBipartite(vector<vector<int>> &graph) {
3 int n = graph.size();
4 vector<int> color(n, 0);
6 for (int i = 0; i < n; i++) {
7 if (color[i]) continue;
8 stack<int> st;
9 st.push(i), color[i] = 1;
10 while (!st.empty()) {
11 int root = st.top();
12 st.pop();
13 for (int c : graph[root]) {
14 if (color[root] == color[c]) return false;
15 if (!color[c]) {
16 st.push(c);
17 color[c] = -color[root];
18 }
19 }
20 }
21 }
22 return true;
23 }
24 };