Generate all subsets + use of Floyd-Warshall (easy explanation)
Since the constraints are very less, we can check every possible subset of nodes.\nFor every subset of nodes we will check if they are forming a subtree then we
Since the constraints are very less, we can check every possible subset of nodes.\nFor every subset of nodes we will check if they are forming a `subtree` then we will calculate the maximum distance between every pair of nodes and accordingly update the answer.\n\nNow to calculate the maximum distance between every pair of nodes,  we can use `Floyd-Warshall Algorithm` .\n\nSo we can precalculate the distance using `Floyd-Warshall`.\n\nNow important thing is, how can we confirm that all nodes of a subset are forming a subtree. We can do by the basic definition of tree(number of nodes - 1 == number of edges).\nNumber of edges can be calculated by calculating the number of pairs of nodes of distance 1.\nHence, for every pair of nodes we check the distance between them, if distance between them is 1 then we increase the count of edges.\nlet\'s say \n![](https://assets.leetcode.com/uploads/2020/09/21/p1.png)\n\nwe are given this tree and in a subset, nodes comes out to be `{4, 3, 2}`. So if we go for every pair then we get pair like this `(4,3), (4,2) , (3,2)`. Out of these pairs, we have (`(4,2)` and `(3,2)`) between which distance between nodes is `1`. Hence in this case we get `2` as number of edges and my subset size is `3`. So it satisfies the condition `number of nodes - 1 == number of edges`, hence it a valid subtree.  \n\n\nAnd while doing so we also keep one `mx` variable to store the maximum distance between them. \n\nCode:- \n```\nvector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        \n        // since we need to calculate the distance for every pair of nodes so we need 2-D vector\n        vector<vector<int>> distance(n + 1, vector<int>(n + 1, 1e9));\n        \n        // Now all the nodes which are directly connected they are having a distance 1 between them\n        for (auto it : edges) {\n            int u = it[0], v = it[1];\n            distance[u][v] = 1;\n            distance[v][u] = 1;\n        }\n        // answer array (using 0-based indexing)\n        vector<int> ans(n-1, 0);\n        \n        // floyd-warshall\n        for (int k = 1;k <= n;k++) {\n            for (int i = 1;i <= n;i++) {\n                for (int j = 1;j <= n;j++) {\n                    distance[i][j] = min(distance[i][k] + distance[k][j], distance[i][j]);\n                }\n            }\n        }\n        \n        \n        // generating all subsets\n        \n        for (int i = 0;i < (1 << n);i++) { \n            vector<int> nodes;\n            for (int j = 0;j < n;j++) {\n                if (i & (1 << j)) {\n                    nodes.push_back(j + 1);// storing the nodes\n                }\n            }\n            \n        \n            \n            int mx = -1, countEdges = 0;\n            // going for each pair of nodes\n            for (int k = 0;k < (int) nodes.size();k++) {\n                for (int l = k + 1;l < (int) nodes.size();l++) {\n                    // if distance between them is 1 it simply means they are connected by an edge.\n                    if (distance[nodes[k]][nodes[l]] == 1) \n                    {\n                        countEdges++;\n                    }\n                    // storing the maximum distance between each pair of nodes\n                    mx = max(mx, distance[nodes[k]][nodes[l]]);\n                }\n            }\n            \n            // if number of edges == number of nodes - 1 => it is tree(basic definition of a tree)\n            if (countEdges == (int) nodes.size() - 1 && mx > 0) { \n                // mx > 0 becoz there can be set of 1 node or 0 node as well, such set won\'t be contributing to answer\n                ans[mx-1]++; // storing the count of each maxDistance. using 0-based indexing (mx - 1)\n            }\n        }\n        \n        return ans;\n    }\n```\n`Time :- O((2^n) * (n^2))`\n
['C', 'Bitmask']