Skip to content

Commit 5940d28

Browse files
authored
Update divide-nodes-into-the-maximum-number-of-groups.cpp
1 parent 0bac9e7 commit 5940d28

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

C++/divide-nodes-into-the-maximum-number-of-groups.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ class Solution {
1010
adj[e[0] - 1].emplace_back(e[1] - 1);
1111
adj[e[1] - 1].emplace_back(e[0] - 1);
1212
}
13-
vector<int> lookup(n);
13+
vector<int> lookup(n, -1);
1414
const auto& iter_dfs = [&](int u) {
1515
vector<int> group;
1616
vector<int> stk = {u};
17-
++lookup[u];
17+
lookup[u] = 0;
1818
while (!empty(stk)) {
1919
const auto u = stk.back(); stk.pop_back();
2020
group.emplace_back(u);
2121
for (const auto& v : adj[u]) {
22-
if (lookup[v]) {
23-
if (lookup[v] % 2 == lookup[u] % 2) { // odd-length cycle
22+
if (lookup[v] != -1) {
23+
if (lookup[v] == lookup[u]) { // odd-length cycle, not bipartite
2424
return vector<int>();
2525
}
2626
continue;
2727
}
28-
lookup[v] = lookup[u] + 1;
28+
lookup[v] = lookup[u] ^ 1;
2929
stk.emplace_back(v);
3030
}
3131
}
@@ -54,7 +54,7 @@ class Solution {
5454

5555
int result = 0;
5656
for (int u = 0; u < n; ++u) {
57-
if (lookup[u]) {
57+
if (lookup[u] != -1) {
5858
continue;
5959
}
6060
const auto& group = iter_dfs(u);

0 commit comments

Comments
 (0)