codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
=begin 분리 집합(DisjointSet) 을 구현한다 =end class DisjointSet attr_reader :data attr_accessor :parent def initialize(data) @parent = nil @data = data end def union_set(set) root = find_set set.parent = root end def find_set root = self root = root.parent while root.parent root end end a = 1;b = 2;c = 3;d = 4 set1 = DisjointSet.new(1) set2 = DisjointSet.new(2) set3 = DisjointSet.new(3) set4 = DisjointSet.new(4) puts "Set1 == Set2 : #{set1.find_set == set2.find_set}" set1.union_set(set3) puts "Set1 == Set3 : #{set1.find_set == set3.find_set}" set3.union_set(set4) puts "Set3 == Set4 : #{set3.find_set == set4.find_set}"
Private
[
?
]
Run code