Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def countComponents(self, n: int, edges: List[List[int]]) -> int:
- def dfs(node: int) -> None:
- if node in visited:
- return
- visited.add(node)
- for nb in hashmap[node]:
- dfs(nb)
- hashmap = defaultdict(list)
- for e in edges:
- a, b = e
- hashmap[a].append(b)
- hashmap[b].append(a)
- visited = set()
- count = 0
- for i in range(n):
- if i not in visited:
- dfs(i)
- count += 1
- return count
Advertisement
Add Comment
Please, Sign In to add comment