Distance in Tree
HardRating 1800CodeforcesAdvanced Trees (BST, LCA, Tree DP)
Approach hintTry it first, then revealHide
Let dp[v][d] be the number of vertices at depth d below v. While merging a child into v, count the pairs whose depths add up to k before adding the child's counts. O(nk).