Rub/Go: Disjoint-Set Union. Leetcode's solution translation.
Leetcode: 1697. Checking Existence of Edge Length Limited Paths.\n\nRuby/Go: Disjoint-Set Union. Leetcode\'s soltuoin translation.\n\nRuby and Go transltion of 
## Leetcode: 1697. Checking Existence of Edge Length Limited Paths.\n\n**Ruby/Go: Disjoint-Set Union. Leetcode\'s soltuoin translation.**\n\nRuby and Go transltion of the Leetcode\'s [solution](https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths/solution/).\n\nRuby code:\n```Ruby\n# Leetcode: 1697. Checking Existence of Edge Length Limited Paths.\n# https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths/\n# = = = = = = = = = = = = = =\n# Accepted.\n# Thanks God, Jesus Christ!\n# = = = = = = = = = = = = = =\n# Runtime: 726 ms, faster than 100.00% of Ruby online submissions for Checking\n# Existence of Edge Length Limited Paths.\n# Memory Usage: 247.5 MB, less than 100.00% of Ruby online submissions for\n# Checking Existence of Edge Length Limited Paths.\n# 2023.04.29 Daily Challenge.\n# @param {Integer} n\n# @param {Integer[][]} edge_list\n# @param {Integer[][]} queries\n# @return {Boolean[]}\nclass Uf\n  def initialize(size)\n    @g = Array.new(size).map.with_index{|x,i| i} \n    @r = []\n  end\n  def find(n)\n    if @g[n] != n\n      @g[n] = find(@g[n])\n    end\n    @g[n]\n  end\n  def join(n1,n2)\n    g1 = find(n1)\n    g2 = find(n2)\n    return if g1 == g2\n    @r[g1] ||= 0\n    @r[g2] ||= 0\n    if @r[g1] > @r[g2]\n      @g[g2] = g1\n    elsif @r[g1] < @r[g2]\n      @g[g1] = g2\n    else\n      @g[g1] = g2\n      @r[g2] ||= 0\n      @r[g2] += 1\n    end\n  end\n  def con(n1,n2)\n    g1 = find(n1)\n    g2 = find(n2)\n    g1 == g2\n  end\nend\ndef distance_limited_paths_exist(n, edge_list, queries)\n  # Based on:\n  # https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths/solution/\n  uf = Uf.new(n)\n  qc = queries.size\n  ans = Array.new(qc,false)\n\n  # Store original inicies with all queries.\n  qwi = [] # Queries with index.\n  qc.times do |i|\n    qwi[i] = queries[i]\n    qwi[i].push(i)\n  end\n\n  # Sort all edges in increasing order of their edge weights.\n  edge_list.sort!{|a,b| a[2] - b[2]}\n  # Sort all queries in increasing order of the limit of edge allowed.\n  qwi.sort!{|a,b| a[2] - b[2]}\n\n  # ei - edges index.\n  ei = 0\n\n  # qoi - Query original index.\n  # l - limit.\n  # Iterate on each query one by one.\n  qwi.each do |pe,q,l,qoi|\n    # We can attach edges with satisfy the limit given by the query.\n    while ei < edge_list.size && edge_list[ei][2] < l\n      n1 = edge_list[ei][0]\n      n2 = edge_list[ei][1]\n      uf.join(n1,n2)\n      ei += 1\n    end\n\n    # If both nodes belong to the same component, it means we can reach them.\n    ans[qoi] = uf.con(pe,q)\n  end\n\n  return ans\nend\n```\n\nGo code:\n```Go\n// Leetcode: 1697. Checking Existence of Edge Length Limited Paths.\n// https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths/\n// = = = = = = = = = = = = = =\n// Accepted.\n// Thanks God, Jesus Christ!\n// = = = = = = = = = = = = = =\n// Runtime: 336 ms, faster than 66.67% of Go online submissions for Checking\n// Existence of Edge Length Limited Paths.\n// Memory Usage: 17.6 MB, less than 33.33% of Go online submissions for Checking\n// Existence of Edge Length Limited Paths.\n// 2023.04.30 Updated.\npackage main\n\n// import "sort"\n// Line 5: Char 8: sort redeclared in this block (solution.go)\n// Line -2: Char 2: other declaration of sort (solution.go)\n// Line 5: Char 8: imported and not used: "sort" (solution.go)\n\ntype unfn struct {\n\tg []int\n\tr []int\n}\n\nfunc (t *unfn) find(n int) int {\n\tif n != t.g[n] {\n\t\tt.g[n] = t.find(t.g[n])\n\t}\n\treturn t.g[n]\n}\n\nfunc (t *unfn) join(n1, n2 int) {\n\tg1, g2 := t.find(n1), t.find(n2)\n\tif g1 == g2 {\n\t\treturn\n\t}\n\tif t.r[g1] > t.r[g2] {\n\t\tt.g[g2] = g1\n\t} else if t.r[g1] < t.r[g2] {\n\t\tt.g[g1] = g2\n\t} else {\n\t\tt.g[g1] = g2\n\t\tt.r[g2] += 1\n\t}\n}\n\nfunc (t *unfn) con(n1, n2 int) bool {\n\tg1, g2 := t.find(n1), t.find(n2)\n\treturn g1 == g2\n}\n\nfunc newUn(n int) *unfn {\n\tx := unfn{}\n\tx.g = make([]int, n)\n\tx.r = make([]int, n)\n\tfor i := 0; i < n; i += 1 {\n\t\tx.g[i] = i\n\t}\n\treturn &x\n}\n\nfunc distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool {\n\n\t// Based on the Leetcode\'s solution.\n\n\tuf := newUn(n)\n\tqc := len(queries)\n\tans := make([]bool, qc)\n\n\t// Store original incides with all quieres.\n\tqwi := make([][]int, qc)\n\tfor i, _ := range qwi {\n\t\tqwi[i] = make([]int, len(queries[i])+1)\n\t\tfor j, v1 := range queries[i] {\n\t\t\tqwi[i][j] = v1\n\t\t}\n\t\tqwi[i][len(queries[i])] = i\n\t}\n\t// Sort all edges in increasing order of their edge weights.\n\tsort.Slice(edgeList, func(i, j int) bool {\n\t\ta, b := edgeList[i], edgeList[j]\n\t\treturn a[2] < b[2]\n\t})\n\t// Sort all queries in increasing orderof the limit of edge allowed.\n\tsort.Slice(qwi, func(i, j int) bool {\n\t\ta, b := qwi[i], qwi[j]\n\t\treturn a[2] < b[2]\n\t})\n\t// ei - edges index.\n\tei := 0\n\te := edgeList\n\tel := len(edgeList)\n\n\t// qoi - Query original index.\n\t// l - limit.\n\t// Iterate on each query one by one.\n\tfor _, v := range qwi {\n\t\tpe, q, l, qoi := v[0], v[1], v[2], v[3]\n\t\tfor ei < el && e[ei][2] < l {\n\t\t\tn1 := e[ei][0]\n\t\t\tn2 := e[ei][1]\n\t\t\tuf.join(n1, n2)\n\t\t\tei += 1\n\t\t}\n\t\t// If both nodes belong to the same component, it means we can reach\n\t\t// them.\n\t\tans[qoi] = uf.con(pe, q)\n\t}\n\n\treturn ans\n}\n```\n
['Go', 'Ruby']