class Calc0092 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n, @m, @k = args.shift.map(&:to_i) @roads = args.shift(@m).map { |l| l.map(&:to_i) } @ds = args.shift.map(&:to_i) end def run h = @roads.group_by { |r| r[2] }.map { |k, v| [k, v.flat_map { |w| [[w[0], w[1]], [w[1], w[0]]] }] }.to_h cands = h[@ds.shift].map(&:last).uniq @ds.each do |d| cands = h[d].select { |w| cands.include?(w.first) }.map(&:last).uniq end [cands.size, cands.sort.join(' ')].join("\n") end end puts Calc0092.new(STDIN.readlines).run if __FILE__ == $0