N, M = gets.split.map &:to_i edges = N.times.map{ [] } M.times{ a, b = gets.split.map &:to_i edges[a-1] << b-1 edges[b-1] << a-1 } Q = gets.to_i Q.times{ a = gets.to_i-1 max_depth = 0 visited = [false] * N f = -> from, depth { return if visited[from] visited[from] = true max_depth = depth if depth > max_depth edges[from].each{|to| f[to, depth + 1] } } if edges[a].empty? puts "0 0" else f[a, 0] puts "#{visited.count(true) - 1} #{max_depth < 2 ? 0 : (max_depth + 1) / 2}" end }