Diameter of every mask
\nclass Solution:\n    def diameter(self, graph, root):\n        n = len(graph)\n        res = 0\n        def d(i, p):\n            nonlocal res\n            s 
```\nclass Solution:\n    def diameter(self, graph, root):\n        n = len(graph)\n        res = 0\n        def d(i, p):\n            nonlocal res\n            s = 1\n            a = b = 0\n            mx = 0\n            for j in graph[i]:\n                if j == p:\n                    continue\n                h, cs = d(j, i)\n                s += cs\n                mx = max(mx, h)\n                t, a, b = sorted([h, a, b])\n            res = max(res, a + b)\n            return mx + 1, s\n        d, size = d(root, -1)\n        return res, size\n    \n    def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:\n        res = [0 for _ in range(n - 1)]\n        for mask in range(1, 1 << n):\n            graph = [[] for _ in range(n)]\n            for u, v in edges:\n                if mask & (1 << (u - 1)) and mask & (1 << (v - 1)):\n                    graph[u - 1].append(v - 1)\n                    graph[v - 1].append(u - 1)\n            d, size = self.diameter(graph, min(i for i in range(n) if mask & (1 << i)))\n            if d > 0 and size == mask.bit_count():\n                res[d - 1] += 1\n        return res\n```
['Python']