BFS colouring
Time O(V + E) Space O(V)color[] = 0 unvisited, ±1 colours. BFS each uncoloured node; conflict → false.
graph = [[1,3],[0,2],[1,3],[0,2]]colour(map)
queue(queue)
Step 1/4Paint node 0 red and put it in the queue.
import java.util.*;
class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int[] color = new int[n];
for (int s = 0; s < n; s++) {
if (color[s] != 0) continue;
color[s] = 1;
Deque<Integer> q = new ArrayDeque<>();
q.offer(s);
while (!q.isEmpty()) {
int u = q.poll();
for (int v : graph[u]) {
if (color[v] == color[u]) return false;
if (color[v] == 0) { color[v] = -color[u]; q.offer(v); }
}
}
}
return true;
}
}Verdict: Standard.