Easy to Understand
\n# Code\n\nclass Solution {\npublic:\n    int dfs_cnt(int node,int mask,vector<vector<int>> &adj,vector<int> &vis){\n        vis[node]=1;\n        int cnt=1;\n
\n# Code\n```\nclass Solution {\npublic:\n    int dfs_cnt(int node,int mask,vector<vector<int>> &adj,vector<int> &vis){\n        vis[node]=1;\n        int cnt=1;\n        for(int it:adj[node]){\n            if(vis[it]==1) continue;\n            if(mask & (1<<it)){\n                cnt+=dfs_cnt(it,mask,adj,vis);\n            }\n        }\n        return cnt;\n    }\n    int diameter(int node,int mask,vector<vector<int>> &adj,vector<int> &vis,int &dm){\n        vis[node]=1;\n        int l=0,r=0;\n        for(int it:adj[node]){\n            if(vis[it]==1) continue;\n            if(mask & (1<<it)){\n                int h=diameter(it,mask,adj,vis,dm);\n                if(h>r){\n                    l=r;\n                    r=h;\n                }else{\n                    l=max(l,h);\n                }\n            }\n        }\n        dm=max(dm,l+r+1);\n        return 1+max(l,r);\n    }\n\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<vector<int>> adj(n);\n        for(int i=0;i<edges.size();i++){\n            int u=edges[i][0],v=edges[i][1];\n            u--;\n            v--;\n            adj[u].push_back(v);\n            adj[v].push_back(u);\n        }\n        vector<int> ans(n-1,0);\n        for(int mask=0;mask<(1<<n);mask++){\n            int cnt=__builtin_popcount(mask);\n            vector<int> vis(n,-1);\n            vector<int> vis1(n,-1);\n            for(int i=0;i<n;i++){\n                if(mask & (1<<i)){\n                    if(dfs_cnt(i,mask,adj,vis)!=cnt) continue;\n                    int dm=0;\n                    int d=diameter(i,mask,adj,vis1,dm);\n                    cout<<mask<<" "<<d<<"\\n";\n                    if(dm-2>=0) ans[dm-2]++;\n                    break;\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']