题目:
题解:
class Solution:def findCircleNum(self, isConnected: List[List[int]]) -> int:def find(index: int) -> int:if parent[index] != index:parent[index] = find(parent[index])return parent[index]def union(index1: int, index2: int):parent[find(index1)] = find(index2)cities = len(isConnected)parent = list(range(cities))for i in range(cities):for j in range(i + 1, cities):if isConnected[i][j] == 1:union(i, j)provinces = sum(parent[i] == i for i in range(cities))return provinces