class Solution:
def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]:
inorder = set()
for pre, cur in edges:
inorder.add(cur)
return [i for i in range(n) if i not in inorder]
class Solution:
def possibleBipartition(self, n: int, dislikes: List[List[int]]) -> bool:
if n == 1 or not dislikes:
return True
blue = 1 # color = Eum(1, -1, 0) # 原始为0
graph = collections.defaultdict(list)
color_dict = collections.defaultdict(int)
for p1, p2 in dislikes:
graph[p1].append(p2)
graph[p2].append(p1)
for i in range(1, n+1):
if not color_dict[i] and not self.dfs(i, blue, graph, color_dict):
return False
return True
def dfs(self, i, color, graph, color_dict):
color_dict[i] = color
for nei in graph[i]:
if color_dict[nei] == color:
return False
if not color_dict[nei] and not self.dfs(nei, -color, graph, color_dict):
return False
return True