C++ O(n * 2^n) solution - bitmask + DFS
Iterate through all subsets: O(2^n) \nDFS: O(n)\nTime complexity: O(n * 2^n)\n```\nclass Solution {\npublic:\n    using Tp = tuple;\n    vector> adj;\n    vecto
Iterate through all subsets: `O(2^n)` \nDFS: `O(n)`\nTime complexity: `O(n * 2^n)`\n```\nclass Solution {\npublic:\n    using Tp = tuple<int, int, int>;\n    vector<vector<int>> adj;\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        adj.resize(n);\n        for (auto &e: edges) {\n            adj[e[0]-1].push_back(e[1]-1);\n            adj[e[1]-1].push_back(e[0]-1);\n        }\n        vector<int> ans(n-1, 0);\n\t\t// iterate through all subsets\n        for (int mask = 1; mask < (1<< n); ++mask) {\n            int x = mask;\n            int root = 0;\n            while (x) {\n                if (x & 1) {\n                    x >>= 1;\n                    break;\n                }\n                ++root;\n                x >>= 1;\n            }\n            if (x == 0) continue; // only one node\n            int subset, max_path, depth;\n            tie(subset, max_path, depth) = subtree_len(mask, root, root);\n            if (subset == mask) { // check all nodes are reachable from root\n                ans[max_path-1]++;\n            }\n        }\n        return ans;\n    }\n    \n    Tp subtree_len(int mask, int x, int p) {\n        int max_path = 0, depth = 0;\n        int max_child_depth = 0, second_child_depth = 0;\n        int subset = (1 << x);\n        for (int to: adj[x]) {\n            if (to != p && (mask & (1<<to))) {\n                int subtree_subset, subtree_max_path, subtree_depth;\n                tie(subtree_subset, subtree_max_path, subtree_depth) = subtree_len(mask, to, x);\n                subset |= subtree_subset;\n                second_child_depth = max(second_child_depth, subtree_depth + 1);\n                if (max_child_depth < second_child_depth) swap(max_child_depth, second_child_depth);\n                max_path = max(subtree_max_path, max_path);\n                depth = max(depth, subtree_depth + 1);\n            }\n        }\n        max_path = max(max_path, depth);\n        max_path = max(max_path, max_child_depth + second_child_depth);\n        return {subset, max_path, depth};\n    }\n};
[]