Ruby - Union-find
Sort edges and queries by weight remembering initial queries order.\n\nIterate queries in weight ascending order. Combain vertices with edges having weight < qu
Sort edges and queries by weight remembering initial queries order.\n\nIterate queries in weight ascending order. Combain vertices with edges having weight < query weight.\n\nFor each query check if vertices are combined (belong to the same group).\n  \n```ruby\ndef distance_limited_paths_exist(n, edge_list, queries)\n  # Union-find\n  par = *0...n # parent vertices for combined groups\n  find = ->(x) { par[x] == x ? x : (par[x] = find[par[x]]) }\n  merge = ->(x, y, _w) { par[find[x]] = find[y] }\n\n  ret = Array.new(queries.size, false)\n\n  edges = edge_list.sort_by(&:last) # sort by weight\n  qs = queries.map.with_index { |(u, v, w), i| [w, u, v, i] }.sort\n  \n  for w, u, v, i in qs\n    merge[*edges.shift] while edges.any? && edges.first.last < w\n    ret[i] = find[u] == find[v]\n  end\n  ret\nend\n```
['Union Find', 'Ruby']