C++ Union-Find and Multimap
Did not see it... tried modified Floyd Warshall - too slow - and then BFS - faster but still too slow.\n\nThe key observation is that we can process queries in 
Did not see it... tried modified Floyd Warshall - too slow - and then BFS - faster but still too slow.\n\nThe key observation is that we can process queries in any order - e.g. from smallest to largest distance. When processing a query, we can use all edges with smaller distances. When processing the next query, we can add more edges, and so on.\n\nDisjoined set structure allows tracking connected sub-sets and joining them efficiently. \n\n```cpp\nint find(vector<int> &ds, int i) {\n    return ds[i] < 0 ? i : ds[i] = find(ds, ds[i]);\n}\nvector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edges, vector<vector<int>>& qs) {\n    multimap<int, array<int, 3>> mm;\n    for (auto i = 0; i < qs.size(); ++i)\n        mm.insert({qs[i][2], {qs[i][0], qs[i][1], i}});\n    sort(begin(edges), end(edges), [](vector<int> &a, vector<int> &b) { return a[2] < b[2]; });\n    vector<int> ds(n, -1);\n    vector<bool> res(qs.size());\n    int ei = 0;\n    for (auto &q : mm) {\n        while (ei < edges.size() && edges[ei][2] < q.first) {\n            int i = find(ds, edges[ei][0]), j = find(ds, edges[ei][1]);\n            if (i != j) {\n                if (ds[i] > ds[j])\n                    swap(i, j);\n                ds[i] += ds[j];\n                ds[j] = i;\n            }\n            ++ei;\n        }\n        res[q.second[2]] = find(ds, q.second[0]) == find(ds, q.second[1]);\n    }\n    return res;\n}\n```
[]