Java - Sort and then Union Find / Disjoint Set
The idea is to sort the queries/edges by distance, then build up a disjoint set with edges allowed for the current query and so on.\n\nRuntime is O(q log(q)) + 
The idea is to sort the queries/edges by distance, then build up a disjoint set with edges allowed for the current query and so on.\n\nRuntime is O(q log(q)) + O(e log(e))\n\n```\n  public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n    // DISJOINT SET\n\n    Map<int[], Integer> original = new IdentityHashMap<>();\n    for (int i = 0; i < queries.length; i++) {\n      original.put(queries[i], i);\n    }\n    \n    \n    // start with smaller queries then union allowed edges and so on\n    parent = IntStream.rangeClosed(0, n).toArray();\n\n    Map<Integer, Boolean> result = new HashMap<>();\n    Arrays.sort(queries, Comparator.comparingInt(a -> a[2]));\n    Arrays.sort(edgeList, Comparator.comparingInt(a -> a[2]));\n    int qI = 0;\n    int eI = 0;\n    for (; qI < queries.length; qI++){\n      int[] query = queries[qI];\n      while (eI < edgeList.length && edgeList[eI][2] < query[2]){\n        union(edgeList[eI][0], edgeList[eI][1]);\n        eI++;\n      }\n      result.put(qI, find(query[0]) == find(query[1]));\n    }\n\n\n    boolean[] r= new boolean[queries.length];\n    for (int i = 0; i < queries.length; i++) {\n      r[original.get(queries[i])] = result.get(i);\n    }\n    return r;\n\n  }\n  \n  private int[] parent;\n\n  private void union(int a, int b) {\n    parent[find(a)] = find(b);\n  }\n\n  private int find(int a) {\n    if (a != parent[a]) {\n      parent[a] = find(parent[a]);\n    }\n    return parent[a];\n  }\n```
[]