C++ || DSU || Easy approach
Here is my c++ code for this problem.\n# Complexity\n- Time complexity:O(V)\n\n- Space complexity:O(V)\n\n# Code\n\nclass disjointset{\n    public:\n    vector<
Here is my c++ code for this problem.\n# Complexity\n- Time complexity:O(V)\n\n- Space complexity:O(V)\n\n# Code\n```\nclass disjointset{\n    public:\n    vector<int>par;\n    disjointset(int n){\n        par.resize(n+1);\n        for(int i=0; i<=n; i++){\n            par[i]=i;\n        }\n    }\n    int findp(int u){\n        if(par[u]==u){return u;}\n        return par[u]=findp(par[u]);\n    }\n    void un(int u, int v){\n        par[findp(u)]=par[findp(v)];\n    }\n};\nclass Solution {\npublic:\n    int makeConnected(int n, vector<vector<int>>& connections) {\n        disjointset d(n);\n        int cnt=0, cnt1=0;\n        for(auto it: connections){\n            if(d.findp(it[0])!=d.findp(it[1])){\n                d.un(it[0], it[1]);\n            }\n            else{\n                cnt++;\n            }\n        }\n        for(int i=0; i<n; i++){\n            if(d.findp(i)==i){cnt1++;}\n        }\n        if(cnt1-1<=cnt){return cnt1-1;}\n        return -1;\n    }\n};\n```
['Union Find', 'Graph', 'C++']