C++ | Submask Enumeration Bruteforce + BFS
time~O(N * 2^N)\nspace~O(N + E)\n\nclass Solution {\npublic:\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector
time~O(N * 2^N)\nspace~O(N + E)\n```\nclass Solution {\npublic:\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<int> res(n-1);\n        vector<vector<int>> adj(n+1);\n        for(auto &a:edges){\n            adj[a[0]].push_back(a[1]);\n            adj[a[1]].push_back(a[0]);\n        }\n        int m=((1<<(n+1))-1)^1;\n        for(int s=m;s;s=(s-1)&m){//2^N\n            if(__builtin_popcount(s)==1)\n                continue;\n            int b,d1;\n            for(int x=1;x<=15;x++){//N\n                if(((s>>x)&1)==1){\n                    b=x;\n                    break;\n                }\n            }\n            int visited=0;\n            int c=1;\n            queue<int> q;\n            visited|=(1<<b);\n            q.push(b);\n            while(q.size()){//N\n                int f=q.front();\n                d1=f;\n                q.pop();\n                for(auto &a:adj[f]){\n                    if(((s>>a)&1)==0 or ((visited>>a)&1)==1)\n                        continue;\n                    visited|=(1<<a);\n                    c++;\n                    q.push(a);\n                }\n            }\n            if(c!=__builtin_popcount(s))\n                continue;\n            c=0;\n            visited=0;\n            visited|=(1<<d1);\n            q.push(d1);\n            while(q.size()){//N\n                int qlen=q.size();\n                while(qlen-->0){\n                    int f=q.front();\n                    q.pop();\n                    for(auto &a:adj[f]){\n                        if(((s>>a)&1)==0 or ((visited>>a)&1)==1)\n                            continue;\n                        visited|=(1<<a);\n                        q.push(a);\n                    }\n                }\n                c++;\n            }\n            res[c-2]++;\n        }\n        return res;\n    }\n};\n```
['Breadth-First Search', 'Queue', 'C', 'Bitmask', 'C++']