C++ O(n * 2 ^ n) solution. bitmask + DFS, with explanation.
\n\nhere is the idea:\n1. iterate all the possible subtree from 1 to (1 << n - 1), the positions of 1 bits are the tree node ID.\n2. use dfs to calcuate the max
\n\nhere is the idea:\n1. iterate all the possible subtree from 1 to (1 << n - 1), the positions of `1` bits are the tree node ID.\n2. use dfs to calcuate the maximum distance for the subtree.\n3. verfiy if the subtree is valid. once we visited a node then we clear the specific bit in the subtree, after finishing dfs, if subtree == 0, that means it is a valid subtree.\n4. update ans by the maximum distance.\n\n```\nclass Solution {\npublic:\n    unordered_set<int> g[16];\n    int dfs(int u, int p, int &subtree, int &mx) {\n        subtree = subtree ^ (1 << (u - 1));  //clear visited Node bit.\n        int a = 0, b = 0;\n        for(auto v : g[u]) {\n            if(v == p) continue;\n            if((subtree & (1 << (v - 1))) == 0) continue; //the next node is not included in the subtree, ignore this node.\n            int res = dfs(v, u, subtree, mx) + 1;\n            if(res > a) {\n                b = a; a = res;\n            } else if(res > b) {\n                b = res;\n            }\n        }\n        mx = max(mx, a + b);    //get the maximum distance in the subtree.\n        return a;  //return the longest path from current node.\n    }\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<int> ans(n - 1);\n        for(auto &e : edges) {     //build graph.\n            g[e[0]].insert(e[1]);\n            g[e[1]].insert(e[0]);\n        }\n        int size = 1 << n;\n        for(int i = 1; i < size; ++i) {\n            if(((i - 1) & i) == 0) continue;  //we don\'t need to calculate the subtree which have one node only.\n            int subtree = i;\n            int u = 0;\n            int mx = 0;\n            for(int j = 0; j < n; ++j) {  // to get the start node.\n                if((1 <<j) & i) {\n                    u = j + 1;\n                    break;\n                }\n            }\n            int res = dfs(u, -1, subtree, mx);\n            if(subtree == 0) {\n                ++ans[mx - 1];\n            }\n        }\n        return ans;\n    }\n};\n```
[]