N, M, K = gets.split.map &:to_i edges = [] M.times.map{ a, b, c = gets.split.map &:to_i edges << [a, b, c] << [b, a, c] } tollages = gets.split.map &:to_i starts = [] edges.each{|a,b,c| starts << a if c == tollages[0] } starts.uniq! checked = [false] * -~N dfs = ->pos, i{ if i == K checked[pos] = true return end edges.each{|a,b,c| next if c != tollages[i] next if a != pos dfs[b, i+1] } } starts.each{|start| dfs[start, 1] } p checked.count(true) puts (1..N).select{|i|checked[i]}*" "