LeetCode 솔루션 분류
[5/20] 785. Is Graph Bipartite?
본문
관련자료
-
링크
댓글 2
JayShin님의 댓글
- 익명
- 작성일
# Time Complexity: O(n + e), Space Compleixty: O(n)
class Solution:
def isBipartite(self, graph: List[List[int]]) -> bool:
n = len(graph)
colors = [0] * n
def dfs(u, color) -> bool:
if (colors[u] != 0):
return colors[u] == color
colors[u] = color
for v in graph[u]:
if (not dfs(v, -color)):
return False
return True
for u in range(n):
if (colors[u] == 0 and not dfs(u, 1)):
return False
return True