Skip to content

Latest commit

 

History

History
43 lines (38 loc) · 1.08 KB

3.md

File metadata and controls

43 lines (38 loc) · 1.08 KB
    void find(vector<bool>& visited,vector<vector<int>>& adj,int& ans,int i)
    {
        visited[i]=true;
        for(auto it:adj[i])
            if(!visited[it])
            {
                find(visited,adj,ans,it);
            }
    }
    int findCircleNum(vector<vector<int>>& isConnected) {
        
        int ans=0;
        vector<bool> visited(isConnected.size(),false);
        int n=isConnected.size();
        vector<vector<int>>adj(n);

       
        for(int i=0;i<isConnected.size();i++)
        {
            for(int j=0;j<isConnected.size();j++)
            {
                if(isConnected[i][j]==1 && i!=j)
                {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
            }
        }
        for(int i=0;i<isConnected.size();i++)
        {
            if(!visited[i])
            {
                ans++;
                find(visited,adj,ans,i);
            }
        }
       
        return ans;
    }