Java Clean Solution
Intuition\nWhen navigating the graph, we use bitwise AND operations between edges to calculate the path\'s cost. Additionally, we can reuse the same edges multi
# Intuition\nWhen navigating the graph, we use bitwise AND operations between edges to calculate the path\'s cost. Additionally, we can reuse the same edges multiple times. By performing bitwise AND, the cost may decrease or remain the same. Hence, our objective is to maximize edge utilization to minimize path cost. We can organize vertices into groups reachable via connected edges. Within each group, traversing any two nodes results in the same cost since we traverse all edges in the group, ensuring the minimum cost. Consequently, for each group of interconnected vertices, the cost remains constant, equivalent to the AND operation of all connecting edges within the group.\n\n# Approach\n- Perform union find on edges to find the groups of connected vertices\n- During the union-find operation, maintain a list called weights to represent the weight of each connected group. When performing a union operation between two vertices, compute the new weight of the group by performing a bitwise AND operation between the existing weight of each group and the cost of the edge connecting them. This process updates the cost of the group of vertices to reflect the addition of the new edge\n- All weights are initialized to 131071. 131071 is chosen because it is 11111111111111111 in binary and is greater than 10^5.\n- When returning result for a query if si is equal to ti return 0 because start and target are same\n- If si and ti don\'t belong to the same group return -1\n- If si and ti are members of the same group, return the result of performing the AND operation on all edges within that group.\n# Complexity\n- Time complexity:$$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass DSU {\n    int[] parent, rank, weights;\n\n    public DSU(int n) {\n        parent = new int[n];\n        rank = new int[n];\n        weights = new int[n];\n        Arrays.fill(weights, 131071);\n        for (int i = 0; i < n; i++)\n            parent[i] = i;\n    }\n\n    public int find(int x) {\n        if (x != parent[x])\n            parent[x] = find(parent[x]);\n        return parent[x];\n    }\n\n    public void union(int x, int y, int weight) {\n        int xx = find(x);\n        int yy = find(y);\n        if (rank[xx] < rank[yy])\n            parent[xx] = yy;\n        else\n            parent[yy] = xx;\n        weights[xx] = weights[yy] = weights[xx] & weights[yy] & weight;\n        if (rank[xx] == rank[yy])\n            rank[xx]++;\n    }\n\n    public int minimum_cost_of_walk(int x, int y) {\n        if (x == y)\n            return 0;\n        if (find(x) != find(y))\n            return -1;\n        return weights[find(x)];\n    }\n}\n\nclass Solution {\n    public int[] minimumCost(int n, int[][] edges, int[][] query) {\n        DSU uf = new DSU(n);\n        for (int[] edge : edges){\n            uf.union(edge[0], edge[1], edge[2]);\n        }\n        \n        int[] result = new int[query.length];\n        for (int i = 0; i < query.length; i++){\n            result[i] = uf.minimum_cost_of_walk(query[i][0], query[i][1]);\n        }\n        return result;\n    }\n}\n```
['Java']