C++ bitmask brute force
```\nclass Solution {\npublic:\n    vector countSubgraphsForEachDiameter(int n, vector>& edges) {\n        g.resize(n);\n        for(auto& e:edges){\n          
```\nclass Solution {\npublic:\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        g.resize(n);\n        for(auto& e:edges){\n            g[e[0]-1].push_back(e[1]-1);\n            g[e[1]-1].push_back(e[0]-1);\n        }\n        \n        vector<int> res(n-1);\n        for(int mask = 1; mask < (1<<n); mask++){\n            int visited = 0, diameter = 0;\n            depth(mask, findAnyIn(mask), visited, diameter);\n            if(visited == mask && diameter>0) res[diameter-1]++;\n        }\n        return res;\n    }\n    \n    // return -1 if invalid subtree\n    int depth(int mask, int i, int& visited, int& diameter) const{\n        add(i, visited);\n        vector<int> vdepth = {0,0}; //add 0 so later don\'t need to check size\n        for(auto j:g[i]){\n            if(in(j, visited) || !in(j, mask)) continue;\n            vdepth.push_back(depth(mask, j, visited, diameter));\n        }\n        sort(vdepth.begin(), vdepth.end(), greater());\n        diameter = max(diameter, vdepth[0] + vdepth[1]);\n        return 1+vdepth[0];\n    }\n    \n    bool in(int i, int mask) const{\n        return (1<<i) & mask;\n    }\n    \n    void add(int i, int& mask) const{\n        mask |= (1<<i);\n    }\n    \n    int findAnyIn(int mask){\n        int i = 0;\n        while(!(mask & 1)){\n            i++;\n            mask >>=1;\n        } \n        return i;\n    }\n    \n    vector<vector<int>> g;\n};
[]