Union-Find
最適化手法は経路圧縮
code:ruby
class UnionFind
# @param {Integer} n
def initialize(n)
@id = Array.new(n, &:itself)
@sz = Array.new(n, 1)
n.times do |i|
end
end
def connected?(i, j)
root(i) == root(j)
end
def unite(i, j)
iroot = root(i)
jroot = root(j)
return if iroot == jroot
else
end
end
private
def root(i)
i
else
@idi = @id[@idi] # pass compression end
end
end