C++ | Subsets| Bitmask
\nclass Solution {\npublic:\n  int res=0;//for each subset max distance\n  int sz=0;//to check whether every node of the subset in connected or not\n  int dfs(i
```\nclass Solution {\npublic:\n  int res=0;//for each subset max distance\n  int sz=0;//to check whether every node of the subset in connected or not\n  int dfs(int node,int mask,vector<vector<int>>&adj){\n    \n    int max_dia=0;//for the current node max diameter i.e max length including the current node\n    sz++;\n      for(auto x:adj[node]){\n        if(mask&(1<<(x-1))){\n          int dia= 1+dfs(x,mask^(1<<(x-1)),adj);\n          res=max(res,dia+max_dia);\n          max_dia=max(max_dia,dia);\n\n        }\n      }\n    return max_dia;\n    \n  }\n  \n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        \n      vector<vector<int>>adj(n+1);\n      for(int i=0;i<edges.size();i++){\n        \n        adj[edges[i][0]].push_back(edges[i][1]);\n        adj[edges[i][1]].push_back(edges[i][0]);\n        \n      }\n      \n      vector<int>v(n-1,0);\n      \n      for(int i=0;i<(1<<n);i++){\n        for(int j=0;j<=n-1;j++){\n          if(i&(1<<j)){\n            res=0;\n            sz=0;\n            dfs(j+1,i^(1<<j),adj);\n            //sz== __builtin_popcount(i) is used to check number of set bits in the current subset\n            if(res>0 && sz== __builtin_popcount(i)){\n            v[res-1]++;\n            }\n            //break  statement is used because we are just picking the first node present in the subset and then simply calculating the longest distance in the current subset \n//therefore doesn\'t make  any sense by calculating longest path from every node as the result will be same for the same subset\n            break;\n          }\n        }\n      }\n      \n      return v;\n      \n      \n    }\n};\n\n```
['C']