bitmask, dfs
Code\n\nclass Solution:\n    def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:\n        graph = {i:set([]) for i in range(1,
# Code\n```\nclass Solution:\n    def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:\n        graph = {i:set([]) for i in range(1,n+1)}\n        for u,v in edges:\n            graph[u].add(v)\n            graph[v].add(u)\n        res = [0]*(n-1)\n        for mask in range(1,2**n):\n            ones = str(bin(mask)).count("1")\n            if ones < 2:continue\n            for j in range(1,n+1):\n                if mask & (1 << (j-1)):\n                    queue = [j]\n                    seen = set([j])\n                    for d in queue:\n                        for dd in graph[d]:\n                            if mask & (1 << (dd-1)) and dd not in seen:\n                                queue.append(dd)\n                                seen.add(dd)\n                    feasible = len(seen) == ones\n                    break\n            if not feasible:\n                continue\n            def helper(node,p):\n                height,longest = 0,0\n                heights = []\n                for subnode in graph[node]:\n                    if mask & (1 << (subnode-1)) and subnode!=p:\n                        subh,subl = helper(subnode,node)\n                        heights.append(-subh)\n                        height = max(height,1+subh)\n                        longest = max(longest,subl)\n                if len(heights)>1:\n                    heights.sort()\n                    longest = max(longest,2-heights[0]-heights[1])\n                longest = max(longest,height)\n                return height,longest\n            for j in range(1,n+1):\n                if mask & (1 << (j-1)):\n                    h,l = helper(j,-1)\n                    res[l-1]+=1\n                    break\n        return res\n\n\n\n\n\n```
['Depth-First Search', 'Bitmask', 'Python3']