Python3 easiest solution
Code\n\nclass Solution(object):\n    def countSubgraphsForEachDiameter(self, n, edges):\n        def bfs(src, cities):\n            visited = {src}\n           
# Code\n```\nclass Solution(object):\n    def countSubgraphsForEachDiameter(self, n, edges):\n        def bfs(src, cities):\n            visited = {src}\n            q = deque([(src, 0)])  # Pair of (vertex, distance)\n            farthestDist = 0 # Farthest distance from src to other nodes\n            while len(q) > 0:\n                u, d = q.popleft()\n                farthestDist = d\n                for v in graph[u]:\n                    if v not in visited and v in cities:\n                        visited.add(v)\n                        q.append((v, d+1))\n            return farthestDist, visited\n\n        def maxDistance(state):  # return: maximum distance between any two cities in our subset. O(n^2)\n            cities = set()\n            for i in range(n):\n                if (state >> i) & 1 == 1:\n                    cities.add(i)\n            ans = 0\n            for i in cities:\n                farthestDist, visited = bfs(i, cities)\n                if len(visited) < len(cities): return 0  # Can\'t visit all nodes of the tree -> Invalid tree\n                ans = max(ans, farthestDist)\n            return ans\n\n        graph = defaultdict(list)\n        for u, v in edges:\n            graph[u-1].append(v-1)\n            graph[v-1].append(u-1)\n            \n        ans = [0] * (n - 1)\n        for state in range(1, 2 ** n):\n            d = maxDistance(state)\n            if d > 0: ans[d - 1] += 1\n        return ans\n```
['Python3']