题目:
代码(首刷看解析):
class Solution {
public:
void dfs(vector<vector<int>>& isConnected,vector<int>& isVis,int i){
int n = isConnected.size();
for(int j=0;j<n;++j){
if(isConnected[i][j]==1 && !isVis[j]){
isVis[j]=1;
dfs(isConnected,isVis,j);
}
}
}
int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
vector<int> isVis(n);
int provinces=0;
for(int i=0;i<n;++i){
if(!isVis[i]){
dfs(isConnected,isVis,i);
provinces++;
}
}
return provinces;
}
};