🧠 Unleash the DSU Magic: How Bitwise AND Reveals the Hidden Minimum Cost!
Intuition
At its core, the solution uses a Disjoint Set Union (DSU) to efficiently group nodes that are connected. The clever twist? Instead of merely checking

## Intuition
![image.png](https://assets.leetcode.com/users/images/8f093dd2-0102-408a-ac55-72f108dad5be_1743523791.1735983.png)
At its core, the solution uses a Disjoint Set Union (DSU) to efficiently group nodes that are connected. The clever twist? Instead of merely checking connectivity, we compute a bitwise AND of all edge weights within each component. This bitwise aggregation acts as the "hidden cost" for every connected subgraph, making each query's answer both unique and instantly accessible.

## Approach
![image.png](https://assets.leetcode.com/users/images/3308abc6-e21c-4e7c-a83e-555543470d07_1743523838.9335568.png)




1. **Union-Find Setup:**  
   Initialize the DSU structure to union nodes based on the given edges. This quickly determines which nodes belong together.

2. **Component Aggregation:**  
   As you traverse the edges, update a dictionary (`comp_and`) to maintain the cumulative bitwise AND of weights for each connected component. This ensures that every component’s aggregated cost is calculated only once.

3. **Query Resolution:**  
   For each query, simply check if the two nodes are in the same component.  
   - **If not connected:** Return `-1`.  
   - **If connected:** Return the precomputed AND value for that component.

This approach is both efficient and elegant, avoiding redundant recalculations and ensuring fast query responses.

## Complexity
- **Time Complexity:**  
  The DSU operations run in almost linear time, roughly $$O(E \cdot \alpha(n))$$ for union-find steps, plus linear processing for queries.

- **Space Complexity:**  
  The solution uses additional space for the DSU parent array and the `comp_and` dictionary, leading to a space complexity of $$O(n + E)$$.

## Code
```python
class DSU:
    def __init__(self, n):
        self.parent = list(range(n))
        
    def find(self, x):
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x
        
    def union(self, a, b):
        ra = self.find(a)
        rb = self.find(b)
        if ra != rb:
            self.parent[rb] = ra

class Solution(object):
    def minimumCost(self, n, edges, query):
        dsu = DSU(n)
        
        # Unite nodes based on edges to form connected components
        for u, v, w in edges:
            if dsu.find(u) != dsu.find(v):
                dsu.union(u, v)
        
        # Compute bitwise AND for each component
        comp_and = {}
        for u, v, w in edges:
            r = dsu.find(u)
            if r in comp_and:
                comp_and[r] &= w
            else:
                comp_and[r] = w
        
        # Answer each query using precomputed results
        res = []
        for s, t in query:
            if dsu.find(s) != dsu.find(t):
                res.append(-1)
            else:
                res.append(comp_and[dsu.find(s)])
        return res
```
<img src="https://assets.leetcode.com/users/images/b91de711-43d7-4838-aabe-07852c019a4d_1743313174.0180438.webp" width="270"/>
['Python']