Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- void dfs(int node, vector<vector<int>>& isConnected, vector<bool>& visit) {
- visit[node] = true;
- for (int i = 0; i < isConnected.size(); i++) {
- if (isConnected[node][i] && !visit[i]) {
- dfs(i, isConnected, visit);
- }
- }
- }
- int findCircleNum(vector<vector<int>>& isConnected) {
- int n = isConnected.size();
- int numberOfComponents = 0;
- vector<bool> visit(n);
- for (int i = 0; i < n; i++) {
- if (!visit[i]) {
- numberOfComponents++;
- dfs(i, isConnected, visit);
- }
- }
- return numberOfComponents;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement