Disjoint set beats 98%
Code
# Code
```cpp []
class Solution {
public:
    vector<int> minimumCost(int n, vector<vector<int>>& edges,
                            vector<vector<int>>& query) {
        UnionFind uf(n);
        for (vector<int> const& edge : edges) {
            uf.unite(edge[0], edge[1], edge[2]);
        }

        vector<int> res;
        for (vector<int> const& q : query) {
            if (uf.areLinked(q[0], q[1])) {
                res.push_back(uf.getCost(q[0]));
            } else {
                res.push_back(-1);
            }
        }
        return res;
    }

private:
    class UnionFind {
    public:
        UnionFind(int len) : mCosts(len, ~0), mParents(len) {
            for (int i = 0; i < len; ++i) {
                mParents[i] = i;
            }
        }

        int getCost(int i) { return mCosts[find(i)]; }

        bool areLinked(int i, int j) { return find(i) == find(j); }

        int find(int x) {
            while (mParents[x] != x) {
                x = exchange(mParents[x], mParents[mParents[x]]);
            }
            return x;
        }

        void unite(int i, int j, int cost) {
            int iParent = find(i);
            int jParent = find(j);
            mCosts[iParent] &= mCosts[jParent] & cost;
            mParents[jParent] = iParent;
        }

    private:
        vector<int> mCosts;
        vector<int> mParents;
    };
};
```
['C++']