N, Q = gets.split(" ").map{|s| s.to_i} $v = Array.new(N) {Array.new} $w = Array.new(N, 1) (N-1).times { a, b = gets.split(" ").map{|s| s.to_i} $v[b-1] << a-1 $v[a-1] << b-1 } q = [] Q.times { q << gets.split(" ").map{|s| s.to_i} } def search(from, to) return 1 if from >= 0 and $v[to].length == 1 $v[to].each {|i| if i != from then n = search(to, i) $w[to] += n end } return $w[to] end search(-1, 0) total = 0 q.each{|p, x| total += $w[p-1] * x puts total }