Read the full problem at the link above. You need to count paths of length exactly , where length is the number of edges. Both endpoints count, so a path from to is one path. Constraints make brute force impossible.
With up to , checking all pairs times out. You need a smarter approach that avoids examining every pair. Try to think about how centroids might help before continuing. What paths pass through a centroid?