Last active
August 29, 2015 14:13
-
-
Save ninoseki/a416d6ac526b63208ec7 to your computer and use it in GitHub Desktop.
Codeforces#285 Div2-B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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