class Solution:
def isBipartite(self, graph: List[List[int]]) -> bool:
color = [-1 for _ in range(len(graph))]
for i, c in enumerate(color):
if c == -1:
queue = deque([i])
color[i] = 0
while queue:
cur_v = queue.popleft()
for next_v in graph[cur_v]:
if color[next_v] == -1:
color[next_v] = color[cur_v] ^ 1
queue.append(next_v)
elif color[next_v] == color[cur_v]:
return False
return True
Python
๋ณต์ฌ