Tree Paths of Length K
Count the unordered pairs of nodes whose tree-path length (number of edges) is exactly K.
Input: the first line has N and K; the next N−1 lines have the edges.
Output: the number of node pairs at distance exactly K.
Constraints:
- 1 ≤ N ≤ 105
- 1 ≤ K ≤ 500
Sample Tests
Input 1
4 2 1 2 2 3 2 4
Output 1
3
Input 2
2 1 1 2
Output 2
1
Discussion (opens after you solve — no spoilers before)
No comments yet.