• + 0 comments

    vector componentsInGraph(vector> gb) {

    int n=gb.size()*2;
    vector<vector<int>>adj(n+1);
    for(int i=0;i<gb.size();i++){
        int u=gb[i][0];
        int v=gb[i][1];
        adj[u].push_back(v);
        adj[v].push_back(u);
    
    }
    vector<bool>visited(n+1,0);
    
    int mini=INT_MAX;
    int maxi=INT_MIN;
    
    for(int i=1;i<=n;i++){
        if(!visited[i]){
            int cnt=0;
             queue<int>q;
            visited[i]=1;
            q.push(i);
            while(!q.empty()){
                int node=q.front();
                q.pop();
                cnt++;
                for(auto j=0;j<adj[node].size();j++){
                    if(!visited[adj[node][j]]){
                        visited[adj[node][j]]=1;
    
                        q.push(adj[node][j]);
                    }
                }
            }
            if(cnt!=1){
            mini=min(mini,cnt);
            maxi=max(maxi,cnt);
            }
    
    
    
    
        }
    }
    vector<int>ans;
    ans.push_back(mini);
    ans.push_back(maxi);
    
    return ans;
    

    }