Runtime beats 98.95%, Memory beats 88.07% [EXPLAINED]
Intuition\nThe problem involves determining if there are paths between nodes in a graph where every edge on the path has a distance below a certain limit. Inste
# Intuition\nThe problem involves determining if there are paths between nodes in a graph where every edge on the path has a distance below a certain limit. Instead of checking each query independently, we can efficiently group the edges and queries based on their weights.\n\n# Approach\nWe sort the edges and queries by distance, then use a Union-Find structure to connect nodes while processing edges that meet the distance criteria, allowing us to quickly check if two nodes are connected for each query.v\n\n# Complexity\n- Time complexity:\nApproximately O((E + Q) * \u03B1(N)), where E is the number of edges, Q is the number of queries, N is the number of nodes, and \u03B1 is the Inverse Ackermann function, which grows very slowly.\n\n- Space complexity:\nO(N) for the Union-Find data structures (parent and rank arrays).\n\n# Code\n```python3 []\nclass Solution:\n    def distanceLimitedPathsExist(self, n, edgeList, queries):\n        # Step 1: Sort edges and queries\n        edgeList.sort(key=lambda x: x[2])  # Sort edges by distance\n        queries = sorted([(p, q, limit, i) for i, (p, q, limit) in enumerate(queries)], key=lambda x: x[2])\n        \n        # Step 2: Prepare Union-Find structure\n        parent = list(range(n))\n        rank = [1] * n\n\n        def find(x):\n            if parent[x] != x:\n                parent[x] = find(parent[x])\n            return parent[x]\n\n        def union(x, y):\n            rootX = find(x)\n            rootY = find(y)\n            if rootX != rootY:\n                if rank[rootX] > rank[rootY]:\n                    parent[rootY] = rootX\n                elif rank[rootX] < rank[rootY]:\n                    parent[rootX] = rootY\n                else:\n                    parent[rootY] = rootX\n                    rank[rootX] += 1\n\n        # Step 3: Process queries\n        result = [False] * len(queries)\n        edgeIndex = 0\n        \n        for p, q, limit, index in queries:\n            # Union all edges with distance < limit\n            while edgeIndex < len(edgeList) and edgeList[edgeIndex][2] < limit:\n                u, v, d = edgeList[edgeIndex]\n                union(u, v)\n                edgeIndex += 1\n            # Check if p and q are in the same component\n            result[index] = find(p) == find(q)\n\n        return result\n\n```
['Python3']