Graph Exploration Target: 15s
2-color BFS: assign opposite colors to neighbors. If same color conflict → not bipartite. Check all components.
from collections import deque
def isBipartite(graph):
n = len(graph)
color = [-1] * n
for start in range(n):
if color[start] != -1: continue
q = deque([start])
color[start] = 0
while q:
u = q.popleft()
for v in graph[u]:
if color[v] == -1:
color[v] = 1 - color[u]
q.append(v)
elif color[v] == color[u]:
return False
return True
Type it from memory. Go.