class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
roots = [i for i in range(n)]
def find(x):
while x != roots[x]:
x = roots[x]
return x
def union(x, y):
rootX = find(x)
rootY = find(y)
if rootX != rootY:
roots[rootX] = rootY
return True
else:
return False
count = n
for x, y in edges:
if union(x, y):
count -= 1
return count