-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution785.cs
48 lines (43 loc) · 1.61 KB
/
Solution785.cs
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
namespace LeetCode.Solutions;
public class Solution785
{
/// <summary>
/// 785. Is Graph Bipartite? - Medium
/// <a href="https://leetcode.com/problems/is-graph-bipartite">See the problem</a>
/// </summary>
public bool IsBipartite(int[][] graph)
{
var colors = new int[graph.Length]; // 0: uncolored, 1: color A, -1: color B
var queue = new Queue<int>(); // Queue for BFS
Array.Fill(colors, 0); // Initialize all nodes as uncolored
// It's possible the graph is not fully connected, so check each node
for (var start = 0; start < graph.Length; start++)
{
// If this node is already colored, it's already checked
if (colors[start] != 0)
{
continue;
}
queue.Enqueue(start); // Start BFS from this node
colors[start] = 1; // Assign a color
while (queue.Count > 0)
{
var node = queue.Dequeue();
foreach (var neighbor in graph[node])
{
// If the neighbor is uncolored, color it with opposite color
if (colors[neighbor] == 0)
{
colors[neighbor] = -colors[node];
queue.Enqueue(neighbor);
}
else if (colors[neighbor] == colors[node])
{
return false; // If the neighbor has the same color, the graph isn't bipartite
}
}
}
}
return true;
}
}