Python disjoint-set union
python []\nclass DSU:\n    def __init__(self, n):\n        self.parent = list(range(n))\n        self.rank = [0] * n\n\n    def find(self, x):\n        if self.
```python []\nclass DSU:\n    def __init__(self, n):\n        self.parent = list(range(n))\n        self.rank = [0] * n\n\n    def find(self, x):\n        if self.parent[x] != x:\n            self.parent[x] = self.find(self.parent[x])\n        return self.parent[x]\n\n    def union(self, x, y):\n        x_root, y_root = self.find(x), self.find(y)\n        if x_root == y_root:\n            return\n        if self.rank[x_root] < self.rank[y_root]:\n            x_root, y_root = y_root, x_root\n        self.parent[y_root] = x_root\n        if self.rank[x_root] == self.rank[y_root]:\n            self.rank[x_root] += 1\n\n\nclass Solution:\n    def distanceLimitedPathsExist(self, n: int, edges: List[List[int]], queries: List[List[int]]) -> List[bool]:\n        queries = [[i] + q for i, q in enumerate(queries)]\n        queries.sort(key=lambda x: x[3])\n        edges.sort(key=lambda x: x[2])\n        dsu = DSU(n)\n        res = [0] * len(queries)\n        j = 0\n        for i, u, v, lim in queries:\n            while j < len(edges) and edges[j][2] < lim:\n                p, q = edges[j][:2]\n                dsu.union(p, q)\n                j += 1\n            res[i] = dsu.find(u) == dsu.find(v)\n        return res\n\n```
['Union Find', 'Sorting', 'Python', 'Python3']