Simple DFS without DSU || Easy to understand✅✅
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
```java []
class Solution {
    class Pair{
        int node;
        int val;
        Pair(int a,int b){
            this.node = a;
            this.val = b;
        }
    }

    List<Pair> adj[];
    Map<Integer,Integer> map = new HashMap();
    void dfs(int src,boolean vis[],int rank,int and[]){
        vis[src] = true;
        map.put(src,rank);
        for(Pair p : adj[src]){
            and[0] &= p.val;
            if(!vis[p.node]){
                dfs(p.node,vis,rank,and);
            }
        }
    }

    public int[] minimumCost(int n, int[][] edges, int[][] query) {
        adj = new ArrayList[n];
        for(int i=0;i<n;i++) adj[i] = new ArrayList();
        for(int i[] : edges){
            int u = i[0]; int v = i[1]; int val = i[2];
            adj[u].add(new Pair(v,val));
            adj[v].add(new Pair(u,val));
        }

        boolean vis[] = new boolean[n];
        int rank=1;
        Map<Integer,Integer> k = new HashMap();
        for(int i=0;i<n;i++){
            if(!vis[i]){
                int and[] = {Integer.MAX_VALUE};
                dfs(i,vis,rank,and);
                k.put(rank,and[0]);
                rank++; 
            }
        }

        int ans[] = new int[query.length]; int p=0;
        for(int i[] : query){
            int u = i[0]; int v = i[1];
            if(!map.get(u).equals(map.get(v))) ans[p++] = -1;
            else{
                int rk = map.get(u);
                ans[p++] = k.get(rk);
            }
        }
        // System.out.print(k);
        return ans;
    }
}
```
['Array', 'Bit Manipulation', 'Depth-First Search', 'Graph', 'Ordered Map', 'C++', 'Java']