DSU Solution|| Explained with Comments || Time and Space Ccomplexity || Easy Beginner Level Solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class DSU{//Dsu class to keep a track on which vertices are part of same components 
//Then just use the normal DSU template 
//Overall Time Complexity :O(NlogN)
    public:
    vector<int>parent;
    vector<int>rank;
    DSU(int n){
        parent.resize(n);
        rank.resize(n);
        for(int i=0;i<n;i++){
            parent[i]=i;
            rank[i]=0;
        }
    }
    int findParent(int node){
        if(parent[node]==node) return node;
        return parent[node]=findParent(parent[node]);
    }
    void Union(int u, int v){
        int a=findParent(u);
        int b=findParent(v);
        if(a==b){return ;}
        if(rank[a]<rank[b]){parent[a]=b;}
        else if(rank[a]>rank[b]){parent[b]=a;}
        else{parent[a]=b;rank[b]++;}
    }

};
class Solution {
public:
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        DSU ds(n);//Initiate DSU 
        //Time:O(N)
        int m=edges.size();
        for(auto i:edges){//For all the edges and vertices create the DSU by filling entries 
        //Time:O(M)
            int u=i[0],v=i[1],w=i[2];
            int a=ds.findParent(u);
            int b=ds.findParent(v);
            if(a==b){continue;}
            else{
                ds.Union(u,v);
            }
        }
        unordered_map<int,int>preAnswer;//For eacg component find the & of all of its edges ,tHis will be the answer for all the vertices in the component 
        //So , I just create a map with the parent name and create answer for all the elements 
        for(auto i:edges){
            int u=i[0],v=i[1],w=i[2];
            int parent=ds.findParent(u);
            if(preAnswer.find(parent)==preAnswer.end()){preAnswer[parent]=w;}//if first then make entry 
            else{preAnswer[parent]=preAnswer[parent]&w;}// if already exist then do a & opertaion 
        }
        vector<int>ans;
        for(auto i:query){//Just traverse the quesry and fetch the answers from the PreAnswer Map
        //Time Needed:O(Query.size())
            int u=i[0],v=i[1];
            int a=ds.findParent(u);
            int b=ds.findParent(v);
            if(a==b){ans.push_back(preAnswer[a]);}
            else{ans.push_back(-1);}
        }
        return ans;

        //Time Complexity:O(Nlogn)+O(N)+O(M)+O(Q)
        //Here i am not explaining the Ackerman Constant , we just ignore it here because it is very minimal 
        //Space Complexity :O(N)
        

        
    }
};
```
['C++']