function calcEquation(equations, values, queries):
parent = {}
weight = {}
function find(x):
if x not in parent:
parent[x] = x
weight[x] = 1.0
if parent[x] != x:
origParent = parent[x]
parent[x] = find(parent[x])
weight[x] *= weight[origParent]
return parent[x]
function union(a, b, val):
rootA = find(a)
rootB = find(b)
if rootA != rootB:
parent[rootA] = rootB
weight[rootA] = weight[b] * val / weight[a]
for i from 0 to equations.length - 1:
(a, b) = equations[i]
val = values[i]
union(a, b, val)
result = []
for each (a, b) in queries:
if a not in parent or b not in parent:
result.add(-1.0)
else if find(a) != find(b):
result.add(-1.0)
else:
result.add(weight[a] / weight[b])
return result
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/data-structures/union-find/evaluate-division-solution
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████████████████████████████████████████████████████████████████