[Javascript] Union Find
Sort the query and edgeLists by limit. Start from the strictest limit.\n\n\nvar distanceLimitedPathsExist = function (n, edgeList, queries) {\n  queries = queri
Sort the query and edgeLists by limit. Start from the strictest limit.\n\n```\nvar distanceLimitedPathsExist = function (n, edgeList, queries) {\n  queries = queries.map((n, i) => [...n, i]).sort((a, b) => a[2] - b[2]);\n  edgeList.sort((a, b) => a[2] - b[2]);\n  \n  const res = Array(queries.length);\n  const ds = new DS(n);\n  \n  let j = 0;\n  for (let [p, q, limit, i] of queries) {\n    while (j < edgeList.length && edgeList[j][2] < limit) {\n      const [u, v, dis] = edgeList[j];\n      ds.union(u, v);\n      j++;\n    }\n    res[i] = ds.find(p) === ds.find(q);\n  }\n  \n  return res;\n};\n\nclass DS {\n  constructor(n) {\n    this.id = [...Array(n).keys()];\n    this.rank = Array(n).fill(0);\n  }\n  \n  find(i) {\n    if (i !== this.id[i]) this.id[i] = this.find(this.id[i]);\n    return this.id[i];\n  }\n  \n  union(i, j) {\n    const [I, J] = [this.find(i), this.find(j)];\n    if (I === J) return false;\n    const [rankI, rankJ] = [this.rank[I], this.rank[J]];\n    if (rankI < rankJ) this.id[I] = J;\n    else if (rankI > rankJ) this.id[J] = I;\n    else {\n      this.id[I] = J;\n      this.rank[J]++;\n    }\n    return true;\n  }\n}\n```
['Union Find', 'JavaScript']