Skip to content

Instantly share code, notes, and snippets.

@ninoseki
Last active August 29, 2015 14:13
Show Gist options
  • Save ninoseki/a416d6ac526b63208ec7 to your computer and use it in GitHub Desktop.
Save ninoseki/a416d6ac526b63208ec7 to your computer and use it in GitHub Desktop.
Codeforces#285 Div2-B
# http://codeforces.com/contest/501/problem/B
q = gets.chomp.to_i
queries = []
q.times { queries << gets.chomp.split }
hash = Hash.new { |h, k| h[k] = [] }
queries.each do |old_name, new_name|
root = hash.keys.select { |key| hash[key].last == old_name }.first
if root.nil?
hash[old_name] << new_name
else
hash[root] << new_name
end
end
puts hash.keys.length
hash.keys.each { |key| puts "#{key} #{hash[key].last}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment