Union Find python
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\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```
['Python3']