classSolution:defcountPairs(self, n:int, edges: List[List[int]])->int:
graph =[[]for _ inrange(n)]for x, y in edges:
graph[x].append(y)
graph[y].append(x)
visi =[False]* n
defdfs(x:int)->int:
visi[x]=True
count =1for y in graph[x]:ifnot visi[y]:
count += dfs(y)return count
res =0for i inrange(n):ifnot visi[i]:
count = dfs(i)
res += count *(n - count)return res //2