✅✅ Using DSU || Beats 100% || Easy Solution
IntuitionThe problem requires us to connect all computers using the given cables, with the constraint that we can only connect two computers if a direct connect
# Intuition
The problem requires us to connect all computers using the given cables, with the constraint that we can only connect two computers if a direct connection exists. If there are fewer cables than `n-1`, it's impossible to connect all computers, so we return `-1`. Otherwise, we use the Disjoint Set Union (DSU) data structure to group computers into connected components and determine the number of additional connections needed.

# Approach
1. **Check Feasibility:** If the number of connections is less than `n-1`, return `-1` since it's impossible to connect all computers.
2. **Initialize DSU:** Create `parent` and `rank` arrays for Union-Find operations.
3. **Connect Components:** Iterate over the connections and use the Union-Find algorithm to merge components.
4. **Count Connected Components:** Every time a `Union` operation successfully merges two previously separate components, decrement the component count.
5. **Compute the Result:** The minimum number of operations required is `components - 1`, as we need one cable to merge two disconnected components.

# Time Complexity
- **Find Operation:** \( O(\alpha(n)) \) (inverse Ackermann function, nearly constant)
- **Union Operation:** \( O(\alpha(n)) \)
- **Processing Connections:** \( O(m \alpha(n)) \), where `m` is the number of connections
- **Total Complexity:** \( O(m \alpha(n) + n) \) ≈ \( O(n) \) for practical inputs

# Space Complexity
- **Parent and Rank Arrays:** \( O(n) \)
- **Total Space Complexity:** \( O(n) \)


# Code
```cpp []
class Solution {
public:
    vector<int>parent;
    vector<int>rank;
    int find(int i){
        if(parent[i]==i) return i;
        return parent[i]=find(parent[i]);
    }
    void Union(int x,int y){
        int parent_x=find(x);
        int parent_y=find(y);
        if(parent_x != parent_y){
            if(rank[parent_x]>rank[parent_y]) parent[parent_y]=parent_x;
            else if(rank[parent_x]<rank[parent_y]) parent[parent_x]=parent_y;
            else{
                parent[parent_y]=parent_x;
                rank[parent_x]++;
            }
        }
    }
    int makeConnected(int n, vector<vector<int>>& connections) {
        if(connections.size()<n-1) return -1;
        parent.resize(n);
        rank.resize(n,0);
        for(int i=0;i<n;i++){
            parent[i]=i;
        }
        int components=n;
        for(auto &vec:connections){
            if(find(vec[0])!=find(vec[1])){
                Union(vec[0],vec[1]);
                components--;
            }
        }
        return components-1;
    }
};
```
# Have a Good Day 😊 UpVote?
['Union Find', 'Graph', 'C++']