[Python/C++] Sort + Union Find
Idea\n Add edges to the graph from smallest to largest.\n Answer queries from smallest to largest.\n Use a Union-Find Data Structure to check connectivity.\n\n\
**Idea**\n* Add edges to the graph from smallest to largest.\n* Answer queries from smallest to largest.\n* Use a [Union-Find Data Structure](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) to check connectivity.\n\n\n**Python**\n```python\nfrom collections import deque\n\nclass Solution:\n    def distanceLimitedPathsExist(self, n, edgeList, queries):\n        q = list(sorted((limit, p, q, i) for i, (p, q, limit) in enumerate(queries)))\n        e = deque(sorted((d, u, v) for (u, v, d) in edgeList))\n        \n        uf = list(range(n))\n        \n        def find(x):\n            if uf[x] != x:\n                uf[x] = find(uf[x])\n            return uf[x]\n        \n        def join(u, v):\n            uf[find(u)] = find(v)\n        \n        def is_connected(u, v):\n            return find(u) == find(v)\n        \n        ans = list(False for _ in range(len(q)))\n        \n        for limit, p, q, i in q:\n            while e and e[0][0] < limit:\n                _, u, v = e.popleft()\n                join(u, v)\n            ans[i] = is_connected(p, q)\n        \n        return ans\n```\n\n**C++**\n```c++\nclass Solution {\npublic:\n    int uf[112345];\n    \n    int find(int x) {\n        return uf[x] != x ? (uf[x] = find(uf[x])) : x;\n    }\n    \n    void join(int x, int y) {\n        uf[find(x)] = find(y);\n    }\n    \n    bool is_connected(int x, int y) {\n        return find(x) == find(y);\n    }\n    \n    vector<bool> distanceLimitedPathsExist(\n            int n, \n            vector<vector<int>>& edges, \n            vector<vector<int>>& queries) {\n        iota(uf, uf + n, 0);\n        \n        for (int i = 0; i < queries.size(); i++) {\n            queries[i].push_back(i);\n        }\n        \n        auto by_weight = [](auto &a, auto &b) {\n            return a[2] < b[2];\n        };\n        \n        sort(queries.begin(), queries.end(), by_weight);\n        sort(edges.begin(), edges.end(), by_weight);\n        \n        vector<bool> ans(queries.size());\n        int j = 0;\n        for (auto &q : queries) {\n            for (; j < edges.size() && edges[j][2] < q[2]; j++) {\n                join(edges[j][0], edges[j][1]);\n            }\n            ans[q[3]] = is_connected(q[0], q[1]);\n        }\n        \n        return ans;\n    }\n};\n```
[]