Union Find + Bitwise
IntuitionFrom the first test case, we observe that it's possible to have different connected components in the graph. This gives us the idea of using the Disjoi
# Intuition
From the first test case, we observe that it's possible to have different connected components in the graph. This gives us the idea of using the Disjoint Set Union (DSU) data structure to efficiently group nodes into components.

Now, since the problem asks for the minimum cost based on the bitwise AND (&) operation, the key observation is:

1. If two nodes are in the same component, we can reach from any node to any other node.
2. Therefore, the minimum possible cost between any two nodes in the same component is simply the bitwise AND of all the edge weights within that component.

This leads to the approach of grouping nodes using DSU and calculating the minimum cost (via bitwise AND) for each component.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Implement DSU (Disjoint Set Union by Size):
    Helps in grouping nodes efficiently and finding which component each node belongs to.
2. Build the Cost Array:

 - Iterate through each edge and:
    Find the root of the component where the edge belongs.
- Compute the bitwise AND of all edge weights within that component.
This cost array will store the minimum cost for each component.

3. Answer Each Query:

- For every query, check:
 a. If both nodes are not in the same component, the answer is -1.
b. If they are in the same component, use the cost array to get the minimum cost for that component.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
  - DSU operations (find and union) are nearly constant time, i.e., O(α(n)) where α is the inverse Ackermann function.
- Processing edges and queries takes O(m + q) where m is the number of edges and q is the number of queries.
- Overall: O(m + q)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
- O(n) for the parent, size, and cost arrays.

# Code
```cpp []
 class Disjoint {
        typedef int ll ; 
        public : 
        vector<ll> parent , size ; 
        Disjoint(ll v){
            parent.resize(v+1 , 0) ; 
            size.resize(v+1 , 0) ; 
            for(ll j = 0 ; j <=v ; j++){
                parent[j] = j ;
                size[j] = 1 ; 
            }
         }
         ll find_parent(ll node){
            if(parent[node] == node){
                return node ;
            }
            return parent[node] = find_parent(parent[node]) ;  
         }
         void union_size(ll a , ll b){
            a = find_parent(a) ; 
            b = find_parent(b) ; 
            if(a==b){
                return ; 
            }
            else if(size[a] <size[b]){
                parent[a] = b ; 
                size[b] = size[a] + size[b] ; 
            }
            else {
                parent[b] = a ; 
                size[a] = size[a] + size[b] ; 
            }
         }
    } ; 
class Solution {
public: 
    typedef int  ll ; 
    
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        Disjoint ds(n) ; 
        vector<ll> ans ; 
        for(ll j = 0 ; j<edges.size() ; j ++){
            ll u = edges[j][0] ; 
            ll v = edges[j][1] ; 
            ds.union_size(u  , v) ; 
        }  
  
        vector<ll> cost(n+1) ;
        for(ll j = 0 ; j<edges.size() ; j ++){
              ll t = ds.find_parent(edges[j][0]) ;
              cost[t] = edges[j][2] ; 
        } 
        for(ll j = 0 ; j<edges.size() ; j ++){
            ll p = ds.find_parent(edges[j][0]) ;
            cost[p]  = (cost[p] & edges[j][2]) ;
        }
        for(ll j = 0 ; j<query.size() ; j ++){
            ll u = query[j][0] ; 
            ll v = query[j][1] ; 
            if(ds.find_parent(u) != ds.find_parent(v)){
                ans.push_back(-1) ; 
            }
            else{
                 ll y = ds.find_parent(u) ;
                ans.push_back(cost[y]) ; 
            }
        }
        return ans ;  
    }
};
```
['Bit Manipulation', 'Union Find', 'C++']