Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def __init__(self):
- self.res_outcome = True
- def dfs(self, visited, graph, i, color):
- # if visited[i] == color:
- # return False
- visited[i] = color
- # print('v: {} visited: {}'.format(i, visited))
- # print('graph: ',graph[i], 'i: ', i, 'visited[i]: ', visited[i])
- for each in graph[i]:
- # print('each: ', each, 'visited[each]: ', visited[each])
- if visited[each] == 0:
- if not self.dfs(visited, graph, each, 3-color):
- return False
- if visited[each] == color:
- return False
- return True
- def isBipartite(self, graph: List[List[int]]) -> bool:
- visited = [0]*len(graph)
- for i in range(len(graph)):
- if visited[i] == 0:
- if not self.dfs(visited, graph, i, 1): # == False
- return False
- # print(visited)
- return True
- # return self.res_outcome
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement