Count Diameters | Codenation

Count Diameters

You are given an undirected tree T consisting of N nodes. For each i from 1 to N-1, find the number of subgraphs of T whose diameter is equal to i.

Note:

  1. A subgraph is defined as a subset of nodes in which every node is reachable from one another using only the nodes from the subset. Two subgraphs are different there is a node iin one of them which is not present in the other. A subgraph of a tree is a tree.

  2. The diameter of a tree is defined as the maximum distance between any two nodes in the tree. The distance between two nodes is defined as the number of edges the simple path between them.

Input Format
The first line of the input consists of a single integer N.
The next N-1 lines consist of two integers u,v denoting an edge between nodes u and v.

Constraints:
2 <= N <= 20

Ouput Format
Print N-1 lines where the ith line consists of the number of subgraphs whose diameter is equal to i,

Sample Input :
3
1 2
2 3

Sample Output:
2
1

Explanation:
The given tree has 7 possible subsets of nodes:[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]. Out of these, the diameter of first three subsets is 0. The diameter of [1,2] and [2,3] is 1 while [1,3] does not form a subgraph because 3 is not reachable from 1 using only the nodes from this subset. The diameter of [1,2,3] is 2

Comments (1)