Simple Solution
\n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \nO(nlogn+qlogq)\n- Space complexity:\n Add your space complexity here, e.g. O(n
\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlogn+qlogq)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n(n+q)\n# Code\n```\nclass UnionFind {\n  public UnionFind(int n) {\n    id = new int[n];\n    rank = new int[n];\n    for (int i = 0; i < n; ++i)\n      id[i] = i;\n  }\n\n  public void unionByRank(int u, int v) {\n    final int i = find(u);\n    final int j = find(v);\n    if (i == j)\n      return;\n    if (rank[i] < rank[j]) {\n      id[i] = id[j];\n    } else if (rank[i] > rank[j]) {\n      id[j] = id[i];\n    } else {\n      id[i] = id[j];\n      ++rank[j];\n    }\n  }\n\n  public int find(int u) {\n    return id[u] == u ? u : (id[u] = find(id[u]));\n  }\n\n  private int[] id;\n  private int[] rank;\n}\n\nclass Solution {\n  public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n    boolean[] ans = new boolean[queries.length];\n    int[][] qs = new int[queries.length][4];\n    UnionFind uf = new UnionFind(n);\n\n    for (int i = 0; i < queries.length; ++i) {\n      qs[i][0] = queries[i][0];\n      qs[i][1] = queries[i][1];\n      qs[i][2] = queries[i][2];\n      qs[i][3] = i;\n    }\n\n    Arrays.sort(qs, (a, b) -> a[2] - b[2]);\n    Arrays.sort(edgeList, (a, b) -> a[2] - b[2]);\n\n    int i = 0; // i := edgeList\'s index\n    for (int[] q : qs) {\n      // Union edges whose distances < limit (q[2])\n      while (i < edgeList.length && edgeList[i][2] < q[2])\n        uf.unionByRank(edgeList[i][0], edgeList[i++][1]);\n      if (uf.find(q[0]) == uf.find(q[1]))\n        ans[q[3]] = true;\n    }\n\n    return ans;\n  }\n}\n```
['Java']