C++ Explaination with comments
\nclass Solution {\npublic:\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        //INT_MAX/2-1 used to avoid overflow\n 
```\nclass Solution {\npublic:\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        //INT_MAX/2-1 used to avoid overflow\n        vector<vector<int>> graph(n, vector<int>(n, INT_MAX/2-1));\n        for (auto e : edges) {\n            int u = e[0] - 1, v = e[1] - 1;\n            graph[u][v] = graph[v][u] = 1;\n        }\n        \n        //floyed-warshall algorithm\n        for (int k = 0; k < n; k++) {\n            for (int i = 0; i < n; i++) {\n                for (int j = 0; j < n; j++) {\n                    graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);\n                }\n            }\n        }\n        \n        vector<int> res(n-1, 0);\n        \n        \n        /* Check for each combination of nodes and see if it\'s a subtree or not.\n        If it\'s a subtree then see maximum distance that can be reached.*/\n        \n        //Each subtree\n        //sub = subtree\n        for (int sub = 0; sub < (1 << n); sub++) {\n            // Number of nodes = nodes\n            int nodes = __builtin_popcount(sub);\n            // edges\n            int edges = 0;\n            //dist = distance\n            int dist = 0;\n            for (int i = 0; i < n; i++)\n                if (sub & (1 << i)) {\n                    for (int j = i + 1; j < n; j++) \n                        if (sub & (1 << j)) {\n                            // if i to j edge increment e\n                            edges += graph[i][j] == 1;\n                            // maximum distance\n                            dist = max(dist, graph[i][j]);\n                    }\n            }\n            // chech its a subtree and distance > 0 if it\'s then its a subtree having max d distance\n            if (edges == nodes - 1 && dist > 0) {\n                res[dist - 1]++;\n            }\n        }\n        \n        return res;\n\t\t//Please upvote\n    }\n};\n```
['Graph', 'C', 'Bitmask']