[Python3] Union-Find
Algo\nSort queries based on weight and sort edges based on weight as well. Scan through queries from lowest to highest weight and connect the edges whose weight
**Algo**\nSort queries based on weight and sort edges based on weight as well. Scan through queries from lowest to highest weight and connect the edges whose weight strictly fall below this limit. Check if the queried nodes `p` and `q` are connected in Union-Find structure. If so, put `True` in the relevant position; otherwise put `False`. \n\n**Implementation**\n```\nclass UnionFind:\n    def __init__(self, N: int):\n        self.parent = list(range(N))\n        self.rank = [1] * N\n\n    def find(self, p: int) -> int:\n        if p != self.parent[p]:\n            self.parent[p] = self.find(self.parent[p])\n        return self.parent[p]\n\n    def union(self, p: int, q: int) -> bool:\n        prt, qrt = self.find(p), self.find(q)\n        if prt == qrt: return False \n        if self.rank[prt] > self.rank[qrt]: \n            prt, qrt = qrt, prt \n        self.parent[prt] = qrt \n        self.rank[qrt] += self.rank[prt] \n        return True \n\n\nclass Solution:\n    def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:\n        queries = sorted((w, p, q, i) for i, (p, q, w) in enumerate(queries))\n        edgeList = sorted((w, u, v) for u, v, w in edgeList)\n        \n        uf = UnionFind(n)\n        \n        ans = [None] * len(queries)\n        ii = 0\n        for w, p, q, i in queries: \n            while ii < len(edgeList) and edgeList[ii][0] < w: \n                _, u, v = edgeList[ii]\n                uf.union(u, v)\n                ii += 1\n            ans[i] = uf.find(p) == uf.find(q)\n        return ans \n```\n\n**Analysis**\nTime complexity `O(NlogN) + O(MlogM)`\nSpace complexity `O(N)`
['Python3']