Bitmask+Floydd Warshall + Dsu
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(2^N *N)\n\n- Space complexity:\nO(N^2)\n\n# Code\n```\nclass Solution {\npublic:\nvoid make(int a,int parent[],int len[])\n{\n    parent[a]=a;\n    len[a]=1;\n}\n\nint find_(int a,int parent[])\n{\n    if(a==parent[a])\n        return a;\n    //path compression\n    int ans=find_(parent[a],parent);\n    parent[a]=ans;\n    return ans;\n}\n\nvoid Union(int a,int b,int parent[],int len[])\n{\n    int x=find_(a,parent);\n    int y=find_(b,parent);\n    if(x==y)\n        return;\n    //union by size\n    if(len[x]<len[y])\n        swap(x,y);\n    parent[y]=x;\n    len[x]+=len[y];\n}\n\n\nbool connected(int n,vector<int>& temp,vector<vector<int>> matrix)\n{\n    int parent[n],len[n];\n    for(int i=0;i<n;i++)\n        make(i,parent,len);\n    for(int i=0;i<temp.size();i++)\n    {\n        for(int j=i+1;j<temp.size();j++)\n        {\n            int a=temp[i],b=temp[j];\n            if(matrix[a][b])\n                Union(a,b,parent,len);\n        }\n    }\n    map<int,int> m;\n    for(int x:temp)\n    {\n        int par=find_(x,parent);\n        m[par]++;\n    }\n    return m.size()==1;\n}\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<vector<int>> matrix(n,vector<int>(n,0));\n        vector<vector<int>> dist(n,vector<int>(n,1e9));\n        for(int i=0;i<n;i++)\n            dist[i][i]=0;\n        for(auto v:edges)\n        {\n            int x=v[0]-1,y=v[1]-1;\n            dist[x][y]=1;\n            dist[y][x]=1;\n            matrix[x][y]=1;\n            matrix[y][x]=1;\n        }\n        for(int k=0;k<n;k++)\n        {\n            for(int i=0;i<n;i++)\n            {\n                for(int j=0;j<n;j++)\n                {\n                    if(dist[i][k]==1e9 || dist[k][j]==1e9)\n                        continue;\n                    dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);\n                }\n            }\n        }\n        vector<int> res(n-1,0);\n        int sz=1<<n;\n        for(int i=1;i<sz;i++)\n        {\n            vector<int> temp;\n            for(int j=0;j<n;j++)\n            {\n                if(i&(1<<j))\n                    temp.push_back(j);\n            }\n            if(temp.size()==1)\n                continue;\n            if(!connected(n,temp,matrix))continue;\n            int maxi=0;\n            for(int a=0;a<temp.size();a++)\n            {\n                for(int b=a+1;b<temp.size();b++)\n                {\n                    maxi=max(maxi,dist[temp[a]][temp[b]]);\n                }\n            }\n            if(maxi)\n                res[maxi-1]++;\n        }\n        return res;\n    }\n};\n```
['C++']