1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public: void dfs(vector<vector<int>>& isConnected, vector<int>& visited, int n, int i) { for (int j = 0; j < n; j++) { if (isConnected[i][j] == 1 && !visited[j]) { visited[j] = 1; dfs(isConnected, visited, n, j); } } }
int findCircleNum(vector<vector<int>>& isConnected) { int n = isConnected.size(); vector<int> visited(n); int circles = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { dfs(isConnected, visited, n, i); circles++; } } return circles; } };
|