Union Set
Code
# Code
```cpp []
class Solution {
public:
    int find(int node, vector<int>& parent){
        if(parent[node]==node) return node;
        return parent[node]=find(parent[node], parent);
    }
    void un(int u, int v,vector<int>& parent, vector<int>& rank){
        int pu=find(u, parent);
        int pv=find(v, parent);
        if(rank[pu]>=rank[pv]){
            parent[pv]=pu;
            rank[pu]++;
        }
        else{
            parent[pu]=pv;
            rank[pv]++;
        }
    }
    
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        vector<int> parent(n);
        iota(parent.begin(), parent.end(), 0);
        vector<int> rank(n, 0);
        vector<int> mincost(n, -1);
        for(auto uvw: edges) un(uvw[0], uvw[1], parent, rank);
        for(auto uvw:edges){
            int pu=find(uvw[0], parent);
            int pv=find(uvw[1], parent);
            int w=uvw[2];
            if(mincost[pu]==-1) mincost[pu]=w;
            else mincost[pu]&=w;
        }
        vector<int> ans;
        for(auto se:query){
            int ps = find(se[0], parent);
            int pe = find(se[1], parent);
            if(ps==pe) ans.push_back(mincost[ps]);
            else ans.push_back(-1);
        }
        return ans;
    }
};
```
['Array', 'Bit Manipulation', 'Union Find', 'Graph', 'C++']