Skip to content

Instantly share code, notes, and snippets.

@naxmefy
Created May 4, 2021 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naxmefy/a62b2bd257c82c63c7dcf404133f6885 to your computer and use it in GitHub Desktop.
Save naxmefy/a62b2bd257c82c63c7dcf404133f6885 to your computer and use it in GitHub Desktop.
find descandents
a=gets.chomp
h=gets.to_i.times.flat_map{s,t=gets.split
{t=>[s],s=>[]}}.reduce{|s,n|s.merge(n){|_,v,w|v+w}}
l=->(r){r+r.flat_map{|a|l[h[a]]}}
r=l[h[a]]
puts r[0]?r.sort: :Nobody
# example
# Emesse
# Lelle Emesse
# Rofl Lelle
# LoLo
# => Lelle + Rofl (but not LoLo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment