Union-Find
Within a connected component of the graph, the minimum cost of traveling between any two nodes is bitwise AND of all included edges.\n\nThis is because we can t
Within a connected component of the graph, the minimum cost of traveling between any two nodes is bitwise AND of all included edges.\n\nThis is because we can traverse all edges of that component as we please.\n\nSo, we find connected components using union-find, tracking the minimum `cost`.\n\nWhen processing queries, we have three cases:\n- The nodes are the same: `0`.\n- The nodes belong to the same component: `cost[parent]`.\n- The nodes belong to different components: `-1`.\n\n**C++**\n```cpp\nvector<int> ds, cost;\nint find(int i) {\n    return ds[i] < 0 ? i : ds[i] = find(ds[i]);\n}\nvector<int> minimumCost(int n, vector<vector<int>>& edges, vector<vector<int>>& query) {\n    ds = cost = vector<int>(n, -1);\n    for (auto &e : edges) {\n        int p1 = find(e[0]), p2 = find(e[1]);\n        if (p1 != p2) {\n            cost[p1] &= cost[p2];\n            ds[p2] = p1;\n        }\n        cost[p1] &= e[2];\n    }\n    vector<int> res;\n    for (auto q : query) {\n        if (q[0] == q[1])\n            res.push_back(0);\n        else if (int p1 = find(q[0]), p2 = find(q[1]); p1 != p2)\n            res.push_back(-1);\n        else\n            res.push_back(cost[p1]);\n    }\n    return res;\n}\n```
['C']