You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
383 B
20 lines
383 B
7 years ago
|
require "set"
|
||
|
h = Hash[ARGF.read.strip.scan(/(\d+) <-> ([\d, ]+)/).map {|a,b| [a.to_i, b.scan(/\d+/).map(&:to_i)]}]
|
||
|
count = 0
|
||
|
until h.empty?
|
||
|
key = h.keys.first
|
||
|
foo = Set.new
|
||
|
foo << key
|
||
|
bar = [key]
|
||
|
until bar.empty?
|
||
|
baz = bar.shift
|
||
|
bar = bar.concat(h[baz] - foo.to_a)
|
||
|
foo = foo.merge(h[baz])
|
||
|
end
|
||
|
count += 1
|
||
|
foo.each do |k|
|
||
|
h.delete(k)
|
||
|
end
|
||
|
end
|
||
|
p count
|