Python, UnionFind, with comments
Sort both edges and queries by distance. For each query connect only nodes closer than the query threshold. All further queries would reuse previous connected g
Sort both edges and queries by distance. For each query connect only nodes closer than the query threshold. All further queries would reuse previous connected graph with possibly adding some own edges.\n\ntime: O(N log(N) + M log(M)) space: O(N)\n\n```\n# standard UnionFind implementation\nclass UnionFind:\n    def __init__(self, n):\n        self.roots = [i for i in range(n)]\n        self.ranks = [1] * n\n        #self.sizes = [1] * n\n        #self.count = n\n\n    def find(self, x):\n        root = self.roots[x]\n        if root != self.roots[root]:\n            self.roots[x] = self.find(root)                \n        return self.roots[x]\n\n    def union(self, x, y):\n        rootx = self.find(x)\n        rooty = self.find(y)\n        if rootx != rooty:\n            if self.ranks[rootx] > self.ranks[rooty]:\n                self.roots[rooty] = rootx\n                #self.sizes[rootx] += self.sizes[rooty]\n            elif self.ranks[rootx] < self.ranks[rooty]:\n                self.roots[rootx] = rooty\n                #self.sizes[rooty] += self.sizes[rootx]\n            else:\n                self.roots[rooty] = rootx\n                #self.sizes[rootx] += self.sizes[rooty]\n                self.ranks[rootx] += 1\n            #self.count -= 1\n\n    def connected(self, x, y):\n        return self.find(x) == self.find(y)\n    \n    #def getCount(self):\n    #    return self.count\n    \n    #def getCompSize(self, x):\n    #    return self.sizes[x]\n    \nclass Solution:\n    def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:\n        \n        m = len(queries)\n        # sort edges by distance\n        edgeList.sort(key=lambda x: x[2])\n        # sort queries added their indexes by distance\n        idx_queries = sorted(zip(queries, range(m)), key=lambda x: x[0][2])\n        \n        res = [False] * m\n        edge_idx = 0\n        \n        uf = UnionFind(n + 1)\n        # review all queries in acsending order\n        for (u, v, t), idx in idx_queries:\n            # add only edges which are less than the current query threshold\n            # only those edges may be reachable for this query\n\t\t\t# they will also automatically be reachable for all further queries\n            while edge_idx < len(edgeList) and edgeList[edge_idx][2] < t:\n                # connect nodes\n                uf.union(edgeList[edge_idx][0], edgeList[edge_idx][1])\n                edge_idx += 1\n            # see if the query nodes connected\n            res[idx] = uf.connected(u, v)\n            \n        return res\n```
[]