0ms | 100%beats | Using Find & Union | Simple Solution
IntuitionApproachComplexity
Time complexity: O(nlogn)

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: O(nlogn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> parent;
    int find(int x){
        if(parent[x] == x) return x;
        return parent[x] = find(parent[x]);
    }

    void Union(int x, int y){
        parent[y] = x;
    }
    vector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {
        
        // store parent and cost of that edge
        parent.resize(n);
        vector<int> cost(n);

        for(int i=0; i<n; i++){
            parent[i] = i;  // initial all node is self parent
            cost[i] = -1; // initial all node cost is -1  because when perform -1&(any number) = any number
        }

        // process each edge and calculate parent and cost...
        for(auto& edge : edges){
            int u = edge[0];
            int v = edge[1];
            int w = edge[2];

            int parent_u = find(u);
            int parent_v = find(v);

            if(parent_u != parent_v){
                cost[parent_u] &= cost[parent_v];
                Union(parent_u, parent_v);
            }

            cost[parent_u] &= w;
        }

        // process query...
        vector<int> result;
        for(auto& que:query){
            int start = que[0];
            int end = que[1];

            int p1 = find(start);
            int p2 = find(end);

            if(start == end) // if both node are equal..
                result.push_back(0);
            else if(p1 != p2)  // if both nodes are not belong to same parent means belong to different components....
                result.push_back(-1);
            else  // if both nodes belong to same parent 
                result.push_back(cost[p1]);
        }
        return result;
    }
};
```
['Array', 'Bit Manipulation', 'Union Find', 'Graph', 'C++']