[Python3] brute force
Enumerate all combinations of edges. In each case, work out the subtrees and their max distance. Collect such info for the statistics. \n\nThis is a pretty cumb
Enumerate all combinations of `edges`. In each case, work out the subtrees and their max distance. Collect such info for the statistics. \n\nThis is a pretty cumbersome algo & implementation. Hopefully, there are better solutions out there. \n\n```\nclass Solution:\n    def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:\n        \n        def fn(edges): \n            """ """\n            graph = {} # graph as adjacency list \n            for u, v in edges:\n                graph.setdefault(u-1, []).append(v-1) # 0-indexed \n                graph.setdefault(v-1, []).append(u-1)\n            group = [None]*n\n            dist = [0]*n\n            \n            def dfs(x, d=0): \n                """ """\n                seen.add(x) # mark visited \n                for xx in graph.get(x, []): \n                    dist[x] = max(dist[x], d)\n                    if group[xx] is None: group[xx] = group[x]\n                    if xx not in seen: dfs(xx, d+1)\n                \n            for i in range(n): \n                seen = set()\n                if group[i] is None: group[i] = i\n                dfs(i)\n            return group, dist \n        \n        ans = {} # answer \n        for r in range(1, len(edges)+1):\n            for x in combinations(edges, r): \n                temp = {}\n                d = {}\n                seen, dist = fn(x)\n                for i in range(n): \n                    temp.setdefault(seen[i], []).append(i)\n                    if seen[i] not in d: d[seen[i]] = dist[i]\n                    else: d[seen[i]] = max(d[seen[i]], dist[i])\n                for k, v in temp.items(): \n                    if len(v) > 1: \n                        ans.setdefault(d[k], set()).add(tuple(sorted(v)))\n        return [len(ans.get(x, set())) for x in range(1, n)]\n```
['Python3']