[python] Dynamic Programming, O(2^N*N)
First calculate all node pairs\' distances, and then iterate all possible states sequentially while each state only require one update, therefore time complexit
First calculate all node pairs\' distances, and then iterate all possible states sequentially while each state only require one update, therefore time complexity is only O(2^N*N)\n```python\nclass Solution:\n    def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:\n        res = [0]*(n-1)\n        g = defaultdict(list)\n        gs = [0]*n\n        dp = [0]*(1<<n)\n        for a, b in edges:\n            a -= 1\n            b -= 1\n            g[a].append(b)\n            g[b].append(a)\n            gs[a] |= (1<<b)\n            gs[b] |= (1<<a)\n            dp[(1<<a)|(1<<b)] = 1\n        dis = [[0]*n for _ in range(n)]\n        def dfs(root, cur, depth, parent):\n            dis[root][cur] = depth\n            for nxt in g[cur]:\n                if nxt == parent:\n                    continue\n                dfs(root, nxt, depth+1, cur)\n            return\n\n        for i in range(n):\n            dfs(i,i,0,-1)\n            \n#         print(dis)\n#         print(ees)\n        \n        for state in range(1<<n):\n            # print(state, dp[state])\n            if dp[state] > 0:\n                res[dp[state]-1] += 1\n                for i in range(n):\n                    if dp[state | (1<<i)] == 0 and (state & gs[i]):\n                        dp[state | (1<<i)] = max(dp[state], max(dis[i][j] for j in range(n) if state&(1<<j)))\n                    # if dp[state | (1<<i)] == 0 and any((i,j) in ees for j in range(n) if state&(1<<j)):\n                    #     dp[state | (1<<i)] = max(dp[state], max(dis[i][j] for j in range(n) if state&(1<<j)))\n                    # if state & (1<<i) == 0 and any((i,j) in ees for j in range(n) if state&(1<<j)):\n                        # dp[state ^ (1<<i)] = max(dp[state ^ (1<<i)], dp[state], max(dis[i][j] for j in range(n) if state&(1<<j)))\n        return res\n\n```
['Dynamic Programming', 'Bitmask', 'Python3']