-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
49 lines (42 loc) · 1.28 KB
/
main.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> group(n, -1);
function<bool(int, int)> dfs = [&](int u, int g) -> bool {
if (group[u] != -1) return group[u] == g;
group[u] = g;
for (int v : graph[u]) {
if (!dfs(v, g ^ 1)) return false;
}
return true;
};
for (int i = 0; i < n; ++i) {
if (group[i] != -1) continue;
if (!dfs(i, 0)) return false;
}
return true;
}
};
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> group(n, 0);
function<bool(int, int)> dfs = [&](int u, int expected_group) -> bool {
if (group[u]) return group[u] == expected_group;
group[u] = expected_group;
int opposite_group = 1;
if (expected_group == 1) opposite_group = 2;
for (int v : graph[u]) {
if (!dfs(v, opposite_group)) return false;
}
return true;
};
for (int i = 0; i < n; ++i) {
if (group[i]) continue;
if (!dfs(i, 1)) return false;
}
return true;
}
};