Holy Moly !. Give it a try for this solution
Hint 1\nForget about multiple edges between two nodes. They are just to confuse. If we want the weight to be less than limitlimitlimit, just check with the mini
# Hint 1\nForget about multiple edges between two nodes. They are just to confuse. If we want the weight to be less than limitlimitlimit, just check with the minimum weight. We can always traverse a\u2194ba \\leftrightarrow ba\u2194b via minimum weighted edge if minWeight<limitmin.\n\n# Hint 2\nNormal traversal from source to destination for each query will be TLE. What is the redundant work? If we could traverse 1\u22122\u22123\u22124 with limit=10, then we can also traverse from any source to destination pair in [1,2,3,4]with limit\u226510.\n\n# Hint 3\nSo, for a specific queries[i], assume there is a copy of the given graph but it has only those edges which has weight<limit. Then we can check if there is a path from source to destination, or in other words, whether source and destination belong to same component or not. What happens when we increase the limitlimitlimit?\n\n# Hint 4\nWhen we were able to traverse 1\u22122\u22123\u22124 with limit=10, suppose there was one edge 1\u21945 with weight=11, and we were not able to use it to reach 555. But if limitlimitlimit is increased to 12, then we can use it to go 1\u22125 and subsequently any source to destination pair in [1,2,3,4,5].\n\n# Hint 5\nWhen limit is increased, we can still use all the edges that were there in our copied graph. But we can use even more edges that has weight where oldLimit\u2264weight<newLimit. We can add these new edges that could not be used earlier, in our copied graph and check for a path again. To do this, we would like to have queries and edgeList sorted with weight or limit. But, before sorting queries, don\'t forget to store their original indices for final result array.\n\n# Hint 6\nLooks like we are still doing traversal in copied graph to check for path on same nodes again and again. How can we quickly check if two nodes belong to the same component in the copied graph? We can make Disjoint Sets of components and check for their parents (See [This](https://leetcode.com/discuss/general-discussion/1072418/Disjoint-Set-Union-(DSU)Union-Find-A-Complete-Guide/) if you want to learn DSU). For an increased limit, take union of those disjoint sets for those new edges.\n\n# Intuition & Approach\n\u2022 problem is to determine if there is a path between two nodes in an undirected graph such that each edge on the path has a distance strictly less than a given limit.\n\n\u2022 The input consists of the number of nodes, the edge list, and the queries.\n\n\u2022 The edge list is an array of arrays, where each sub-array represents an edge between two nodes and the distance between them.\n\n\u2022 The queries are an array of arrays, where each sub-array represents a query with two nodes and a limit.\n\n\u2022 The using the union-find algorithm to determine if there is a path between two nodes.\n\n\u2022 The union-find algorithm is used to group nodes into connected components.\n\n\u2022 First sorts the edges in the edge list by their distance in ascending order.\n\n\u2022 Then iterates over the sorted edges and performs a union operation on the nodes connected by the edge.\n\n\u2022 The union operation updates the parent and rank arrays to group the nodes into connected components.\n\n\u2022 Then iterates over the queries and checks if the two nodes are connected and if the distance between them is less than the given limit.\n\n\u2022 The isConnectedAndWithinLimit method uses the find method to determine if the two nodes are connected and if the distance between them is less than the given limit.\n\n\u2022 The find method uses the parent and weight arrays to find the root of the connected component and checks if the weight of the edge is less than the given limit.\n\n\u2022 The union method updates the parent, rank, and weight arrays to group the nodes into connected components and store the weight of the edge.\n\n\u2022 finally returns a boolean array where each element represents if there is a path between the two nodes in the corresponding query with edges less than the given limit.$ -->\n\n# Code\n```\nclass UnionFind:\n    def __init__(self, size):\n        self.parent = [i for i in range(size)]\n        \n    def union(self, a, b):\n        self.parent[self.find(a)] = self.find(b)\n        \n    def find(self, a):\n        if self.parent[a] != a:\n            self.parent[a] = self.find(self.parent[a])\n        return self.parent[a]\n\nclass Solution:\n    def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:\n        E = len(edgeList)\n        Q = len(queries)\n        uf = UnionFind(n)\n        ans = [False] * Q\n        for i in range(Q):\n            queries[i].append(i)\n        queries.sort(key=lambda x: x[2])\n        edgeList.sort(key=lambda x: x[2])\n        j = 0\n        for i in range(Q):\n            while j < E and edgeList[j][2] < queries[i][2]:\n                uf.union(edgeList[j][0], edgeList[j][1])\n                j += 1\n            ans[queries[i][3]] = (uf.find(queries[i][0]) == uf.find(queries[i][1]))\n        return ans\n\n\n"""\n# TLE and wrong ans\n\nclass Solution:\n    def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:\n        parents = defaultdict(lambda: None)\n        sizes = defaultdict(int)\n        \n        def find(a):\n            if a not in parents:\n                parents[a] = a\n                sizes[a] = 1\n                return a\n\n            root = a\n            while parents[root] != root:\n                root = parents[root]\n            while parents[a] != a:\n                nxt = parents[a]\n                parents[a] = root\n                a = nxt\n            return root\n        \n        def union(a,b):\n            roota = find(a)\n            rootb = find(b)\n            if roota == rootb:\n                return\n            if sizes[roota] > sizes[rootb]:\n                sizes[roota] += sizes[rootb]\n                sizes[rootb] = 0\n                parents[rootb] = roota\n            else:\n                sizes[rootb] += sizes[roota]\n                sizes[roota] = 0\n                parents[roota] = rootb\n\n        Q, E = len(queries), len(edgeList)\n\n        #print(edgeList)      \n        edgeList.sort(key=lambda x: x[2])\n        #print(edgeList)\n        queries = sorted(((u,v,l,idx) for idx,(u,v,l) in enumerate(queries)), key=lambda x: x[2])\n        #print(queries)\n\n        res = [False] * Q\n        e_idx = 0\n        for p, q, l, i in queries:\n            while e_idx < E and edgeList[e_idx][2] < l:\n                u, v, _ = edgeList[e_idx]\n                union(u, v)\n                res[i] = find(p) == find(q)\n                e_idx += 1\n        return res\n"""\n```
['Array', 'Union Find', 'Graph', 'Sorting', 'Python3']