A O(2^n*nlogn) solution Bitmask to find all possibilities and the final diameter of the graph.
Intuition\nWe will check all possibilities of picking up a subset of nodes.\nNow we will call the find diameter fn for finding the diameter which will use a pri
# Intuition\nWe will check all possibilities of picking up a subset of nodes.\nNow we will call the find diameter fn for finding the diameter which will use a priority_queue to maintain all the max depths from that node.\nSo the first and the runnerUp will give the max diameter of that subset.\nWe will mark visited with the help of mask as well.\nIf the mask is not 0 we will discard that subset from being counted.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> parent;\n    int findParent(int u) {\n        if(parent[u] == -1)return u;\n        return findParent(parent[u]);\n    }\n    void unite(int u, int v) {\n        int parA = findParent(u), parB = findParent(v);\n        parent[parB] = parA;\n    }\n    int findDiameter(vector<int> adj[], int root, int &mask, int &maxDiam) {\n        // cout<<root<<endl;\n        mask ^= (1<<(root-1));\n        priority_queue<int> distances;\n        for(auto child:adj[root]) {\n            if(!((mask>>(child-1))&1))continue;\n            // cout<<child<<" "<<root<<endl;\n            distances.push(findDiameter(adj, child, mask, maxDiam)+1);\n        }\n        int topDi = distances.empty()?0:distances.top();\n        if(!distances.empty())distances.pop();\n        int runnerUpDi = distances.empty()?0:distances.top();\n        maxDiam = max(maxDiam, topDi+runnerUpDi);\n        // cout<<topDi<<" "<<runnerUpDi<<" "<<maxDiam<<" "<<root<<endl;\n        return topDi;\n    }\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<int> adj[n+1];\n        int degrees[n+1];\n        memset(degrees, 0, sizeof degrees);\n        for(auto edge:edges) {\n            adj[edge[0]].push_back(edge[1]);\n            adj[edge[1]].push_back(edge[0]);\n            degrees[edge[1]]++;\n        }\n\n        vector<int> distancesMap(n-1);\n        for(int i=3; i < (1<<n); i++) {\n            int maxDistance = 0;\n            int root = 0;\n            int mask = i;\n            int j = mask;\n            while(j) {\n                if((j&1))break; \n                j>>=1;\n                root++;\n            }\n            if(root+1<=n)\n                findDiameter(adj, root+1, mask, maxDistance);\n            if(maxDistance>0 && mask == 0)\n                distancesMap[maxDistance-1]++;\n\n            // if(__builtin_popcount(i) == 2)\n            //     cout<<i<<" "<<root<<" "<<mask<<" "<<maxDistance<<endl;\n        }\n        return distancesMap;\n    }\n};\n```
['C++']