C++|| line by line explanation || DFS
Complete And Clean Code is in last\n\n# Intuition\nfrom the quesion , we can say we need to find the count of group of computer which are not connected to any o
# Complete And Clean Code is in last\n\n# Intuition\nfrom the quesion , we can say we need to find the count of group of computer which are not connected to any other computer ( they may connected in group but does not coonected to another group)\n\nAlso we require to count wires, that are availble ,\nnumber of wire can be easly find by given number of connection\n```\nint m = con.size();\n```\n\n# Approach\nTo start solving quetion , we need to know , which computer is connected to which one , in short we need a adjency list \n\nlet\'s create adjency list.......\n```\nvector<int> adj[n];\n  for(auto a : con){\n    adj[a[0]].push_back(a[1]);\n    adj[a[1]].push_back(a[0]);\n  }\n``` \nonce we have adjency list we can use DFS to travell along all the computer that are connected to each other.\nAs we have N numbers of computer , let\'s make a array that will tell use given index of computer is visted or not , if a computer is vister that mean have already counted it.\n```\nvector<int> vis(n,0);\n```\nHere comes main concept .. if i start to travell from a computer at index 0 , i can travell along all the computers that are directly or indirectly connected to index 0 computer , while traversing we will mark as visted so that we are not come to visit any computer again.\n\nOnce a group of compter visted and marked as visted , we will try to find another group of computer that are not visted , repeating this process we can find numbers of group of computer that are not connected.\n```\nint c=0;\nfor(int i =0; i<n;i++){\n  if(!vis[i]){\n    c++;\n    dfs(adj,vis,i);\n  }\n}\n```\nhere is our DFS fuction\n```\nvoid dfs(vector<int> adj[] , vector<int> &vis , int i){\n  vis[i]=1;\n  for(auto a : adj[i]){\n    if(!vis[a]){\n      dfs(adj,vis,a);\n    }\n  }\n}\n```\n\nAt last we will check either number of wire is more that our requirement , to connect N numbers of computer we needd atleast N-1 wires , here "m" is numbers of wires .\n```\nif(m>=n-1) return c-1;\nreturn -1;\n```\nYE KARNA BHI IMPORTANT H\n\n```\nREAD IT ? DO A VOTE UP...\n```\n\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\npublic:\n\n    void dfs(vector<int> adj[] , vector<int> &vis , int i){\n        vis[i]=1;\n        for(auto a : adj[i]){\n            if(!vis[a]){\n                dfs(adj,vis,a);\n            }\n        }\n    }\n\n    int makeConnected(int n, vector<vector<int>>& con) {\n        vector<int> adj[n];\n        vector<int> vis(n,0);\n        for(auto a : con){\n            adj[a[0]].push_back(a[1]);\n            adj[a[1]].push_back(a[0]);\n        }\n        int c=0;\n        int m = con.size();\n        for(int i =0; i<n;i++){\n            if(!vis[i]){\n                c++;\n                dfs(adj,vis,i);\n            }\n        }\n        if(m>=n-1) return c-1;\n        return -1;\n    }\n};\n```
['Depth-First Search', 'Graph', 'C++']