This problem is an interesting exercise on centroid decomposition. In fact it's one of the earliest problems which appeared involving centroid decomposition before it become a popular algorithm in CP contests. If you are not familiar with centroid decomposition you better refer to this tutorial Centroid Decomposition in Trees Tutorial , after that you should solve this simple problem Codeforces 321C: Ciel The Commander . After that you should be able to understand the solution.

Hint 2:

After finding the centroid of our tree, if we process all paths passing by this centroid, then we would be able to remove this centroid and break our tree into smaller independent subtrees (each with size not exceeding half of our big tree's size). After that we can solve each of our small subtrees independently.

Hint 3:

Now let's tell about processing all paths passing by a fixed centroid. First of all let's consider all children of our centroid (of course without ones that had been processed as centroids, because we found all paths passing by them, and doing that again would count duplicates and may also lead to TLE). Consider our centroid children (c1,c2,c3,c4..ck). Any simple path passing by the centroid would be a union of 2 paths ({centroid->v} , {centroid->u} ). where (v,u) are 2 nodes belonging to 2 different subtrees rooted at 2 different children of our centroid. If they belong to the subtree rooted at the same centroid's child, then the path won't be simple.

Hint 4:

Let's process the centroid's children subtrees one by one. For each of them we call a DFS starting from the centroid to this subtree nodes. For each path from the centroid to a node belonging to this subtree, we should calculate the weight of this path, and the number of edges forming this path. If our path's weight is equal to x then it should be joined with another path starting at the centroid with total weight K-x, and of course formed with minimum number of edges.

Hint 5:

We should maintain an array E[K]. E[i] represents the minimum number of edges forming a path of weight i (among all processed paths before). So for each subtree we should first try to join paths to nodes in this subtree with previous results. After that we may add them to our array E[]. After finishing each centroid we should reset our array E[]. Check my implementation for details.

Ususally in Competitive programming tasks are designed so that you are able to solve them in any structure you would like to. What I mean that it doesn't matter what linear data structure you use, all of them would be able to get the problem. But sometimes if the intended solution is to use linear memory, you won't be able to have quadratic consumption in your code. So don't worry, Alot of topcoders use only STL structures (vector for example) instead of arrays.

This problem is similar to the problem of counting different ways of partitioning a number into a summation of smaller numbers. Here we are partitioning the number into multiplication of divisors.

Hint 3:

Let's define a recursive function f(x) which represents the number of different ways to partition number x. If our number is divisible by 2 then f(x) = f(x/2) since if we append 2 to each partition of x/2 that would be a new partition of f(x). If our number is also divisible by 3 then f(x) = f(x/2) + f(x/3) since if we append 3 to each partition of x/3 that would be a new partition of f(x). Note that the new partitions we have added are different than partitions of f(x/2) which we appended 2 to each of them (Try to deduce the recursive function, this was a big hint).

Calculating the distance between every pairs of nodes is a solution that will get TLE for sure. Let's try to think of a better solution. Observe that each edge would be included in the distance of each pair of nodes such that this is edge is a part of the path between them. So let's write our answer in another way.

Let's pick an arbitrary root for our tree and start a Depth First Search from this root. Let's keep the size of each subtree. Let's define an array sz[] where sz[i] denotes the size of the subtree rooted at the i-th node.

Hint 3:

Assume that we have an edge that connects 2 nodes (u , v) such that u is parent of v in our rooted tree. So the number of paths such that this edge is a part of is equal to (sz[v] * (n - sz[v]))

It's obvious that number 1 must be an element of our set. First of all let's sort numbers of our set in increasing order and insert number 1 if it doesn't exist.

Hint 2:

Let's process our elements one by one in increasing order, assume that using the first i elements we are able to form all numbers in the range [1,X] , what should be our next number?

Hint 3:

If the next number Y is less than or equal to X+1 , then we can simply extend our range of formed number to [1, X+Y+1].

Hint 4:

If the next element is greater than X+1, then we must insert X+1 into our set.We mentioned that we are able to form all numbers in the range [1,X], if all upcoming numbers are greater than X+1 , then we won't be able to form X+1 in anyway. After insertion our range would be extended to [1,X+X+1].