C# bitmask + Floyd–Warshall
Intuition\nEnumerate all subsets of vertexes.\nFor each subset check if this forms a tree when adding edges.\nRun Floyd-Warshall and capture maximum distance. \
# Intuition\nEnumerate all subsets of vertexes.\nFor each subset check if this forms a tree when adding edges.\nRun Floyd-Warshall and capture maximum distance. \nAdd 1 to the corresponding array index\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```csharp []\npublic class Solution {\n    public int[] CountSubgraphsForEachDiameter(int n, int[][] edges) \n    {\n        var result = new int[n - 1];\n        for(int mask = 1; mask <= (1 << n); ++mask)\n        {\n            var vertexes = new HashSet<int>();\n            var matrix = new long[n, n];\n            var edgesCount = 0;\n\n            for(int i = 0; i < n; ++i)\n            {\n                if((mask & (1 << i)) > 0)\n                {\n                    vertexes.Add(i);\n                }\n            }\n            for(int i = 0; i < n; ++i)\n            {\n                for(int j = 0; j < n; ++j)\n                {\n                    if(i != j)\n                    {\n                        matrix[i, j] = int.MaxValue;\n                    }\n                }\n            }\n\n            foreach(var edge in edges)\n            {\n                var start = edge[0] - 1;\n                var end = edge[1] - 1;\n                if( vertexes.Contains(start) && \n                    vertexes.Contains(end) )\n                    {\n                        ++edgesCount;\n                        matrix[start, end] = 1;\n                        matrix[end, start] = 1;\n                    }\n            }\n\n            if(vertexes.Count - 1 == edgesCount && edgesCount > 0)\n            {\n                for(int k = 0; k < n; ++k)\n                {\n                    for(int i = 0; i < n; ++i)\n                    {\n                        for(int j = 0; j < n; ++j)\n                        {\n                            matrix[i, j] = Math.Min(matrix[i, j], matrix[i, k] + \n                                                                matrix[k, j]);\n                        }\n                    }\n                }\n\n                var maxDistance = 0;\n                for(int i = 0; i < n; ++i)\n                {\n                    for(int j = 0; j < n; ++j)\n                    {\n                        if(matrix[i, j] != int.MaxValue)\n                        {\n                            maxDistance = (int)Math.Max(maxDistance, matrix[i, j]);\n                        }\n                    }\n                }\n                 \n                if(maxDistance > 0 && maxDistance <= n - 1)\n                {\n                    result[maxDistance - 1] += 1;        \n                }\n            }\n        }\n\n        return result;\n    }\n}\n```
['C#']