class Yukicoder def initialize n, m, k = gets.chomp.split(' ').map(&:to_i) paths = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = []}} m.times do a, b, c = gets.chomp.split(' ').map(&:to_i) paths[a][c] << b paths[b][c] << a end d = gets.chomp.split(' ').map(&:to_i) current = [*(1..n)] d.each do |cost| temp = [] current.each do |i| temp |= paths[i][cost] end current = temp.dup end puts current.size puts current.sort.join(' ') end end Yukicoder.new