题目描述
There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.
A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.
Return the total number of provinces.
Constraints:
1 <= n <= 200
n == isConnected.length
n == isConnected[i].length
isConnected[i][j] is 1 or 0.
isConnected[i][i] == 1
isConnected[i][j] == isConnected[j][i]
题目分析
这道省份的题目就是以前的朋友圈问题。
如果城市a能去到城市b, 他们之间就是直接连接的城市;如果城市b还能去到城市c,那么城市a和城市c之间就是间接连接的城市。
一个省就是所有能互相连通的城市的集合。
现在我们有一个n乘n的矩阵 isConnected[i][j] == 1
代表着 i 城市和 j 城市之间是直接连接的