C++ | Best Code | Best Explanation
Short Explanation\n For all subsets check:\n\t If it is valid subtree or not ?\n\t\t If it is valid subtree then find the maximum distance between two nodes.\n\
**Short Explanation**\n* For all subsets check:\n\t* If it is valid subtree or not ?\n\t\t* If it is valid subtree then find the maximum distance between two nodes.\n\t\t* Let ```ans[n-1]``` is the return vector and let maximum distance be ```dist```, then add one to ```ans[dist-1]``` (Because it is now 1-based indexing). \n\n\n**Detailed Explanation**\n* Created adjacency matrix named ```vector<vector<int>>adj``` using function ```makeAdjMatrix()```.\n* Used function ```preComputeDist()```  (Floyd Warshall Algorithm) to calculate shortest distance from each node to each other node and stored it in ```vector<vector<int>>dist```.\n* Then I generated all subsets using bit manipulation and for every subset:\n\t* If the size of subset is greater than 1 and it is ```validSubtree```\n\t\t* I calculated maximum distance in that subtree by checking distance of each node with each other node.\n\n**Checking the subtree validity of given subset**\n* Create a ```vis[n+1]``` array. \n\t* ```vis[i]=0``` means ```ith``` node is not present in subset.\n\t* ```vis[i]=1``` means ```ith``` node is present in subset.\n\t* ```vis[i]=2``` means ```ith``` node is visited when doing ```dfs``` on the subtree.\n*   We do dfs from any node given in subset, and we only traverse further to neighbouring node if that node is present in given subset. And we stop when we don\'t have any options left.\n*   Using above method, if the subset is valid, all the ```ith``` nodes that has initially ```vis[i]==1``` will now have ```vis[i]==2```. And hence the summation of all the elements in ```vis[]``` will become ```2*size of provided subset```. Then we will return true, else we will return false.\n\n```\nclass Solution {\nprivate:\n    vector<vector<int>>dist;\n    vector<vector<int>>adj;\n    \n\t/*\n\tFloyd Warshall method to calculate shortest distance \n\tfrom each node to each other node.\n\t@params int n\n\t@params vector<vector<int>>edges\n\t@return void\n\t*/\n    void preComputeDist(int n, vector<vector<int>>edges){\n        dist = vector<vector<int>>(n+1,vector<int>(n+1,INT_MAX));\n        for(auto v:edges){\n            dist[v[0]][v[1]]=1;\n            dist[v[1]][v[0]]=1;\n        } \n        \n        for(int k=1;k<=n;k++){\n            for(int i=1;i<=n;i++){\n                for(int j=1;j<=n;j++){\n                    if(i==j){\n                        dist[i][j]=0;\n                    }else{\n                        if(dist[i][k]!=INT_MAX && dist[k][j]!=INT_MAX){\n                            dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);\n                        }\n                    }\n                }\n            }\n        }\n        \n        return;\n    }\n    \n\t/*\n\tFinds distance from each node of set,\n\tto each other node of set and \n\treturns the maximum of all distance\n\t@params set<int>s\n\t@return int\n\t*/\n    int maxDist(set<int>s){\n        int ans=0;\n        for(auto it1:s){\n            for(auto it2:s){\n                ans = max(ans,dist[it1][it2]);\n            }\n        }\n        return ans;\n    }\n    \n\t/*\n\tCreates adjacency matrix from the given edges.\n\t@params int n\n\t@params vector<vector<int>>edges\n\t@return void\n\t*/\n    void makeAdjMatrix(int n, vector<vector<int>>edges){\n        adj = vector<vector<int>>(n+1);\n        for(auto v:edges){\n            adj[v[0]].push_back(v[1]);\n            adj[v[1]].push_back(v[0]);\n        }\n        return;\n    }\n    \n\t/*\n\t@params int node\n\t@params int par\n\t@params int[] vis\n\t@return void\n\t*/\n    void dfs(int node, int par, int *vis){\n        for(auto it:adj[node]){\n            if(it!=par && vis[it]){\n                vis[it]=2;\n                dfs(it, node, vis);\n            }\n        }\n    }\n    \n\t/*\n\tFor a given subset, it returns true if the subset is\n\tvalid subtree or not.\n\t@params int n\n\t@params set<int>s\n\t@return bool\n\t*/\n    bool validSubtree(int n, set<int>s){\n        int start = *s.begin();\n        int vis[n+1];\n        memset(vis,0,sizeof(vis));\n        for(auto it:s){\n            vis[it]=1;\n        }\n        vis[start]=2;\n        dfs(start, -1, vis);\n        int cnt=0;\n        for(int i=1;i<=n;i++)cnt+=vis[i];\n        if(cnt==2*s.size())return true;\n        return false;\n    }\n    \npublic:\n\n\t/*\n\t@params int n\n\t@params vector<vector<int>>edges\n\t@return vector<int>\n\t*/\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<int>ans(n-1);\n        \n\t\t//create adj matrix\n        makeAdjMatrix(n,edges);\n\t\t\n\t\t//store shortes dist from each \n\t\t//node to each other node\n        preComputeDist(n,edges);\n        \n\t\t//generate all subsets.\n        for(int i=0;i<(1<<n);i++){\n            set<int>s;\n            for(int j=0;j<n;j++){\n                if(i&(1<<j)){\n                    s.insert(j+1);\n                }\n            }\n\t\t\t\n\t\t\t//for each generated subsets,\n\t\t\t//check if it is valid subset or not.\n            if(s.size()>1 && validSubtree(n,s)){\n\t\t\t\n\t\t\t\t//find the maximum distance (Maximum diameter)\n\t\t\t\t//of the subset.\n                int dist = maxDist(s);\n                ans[dist-1]++;\n            }\n        }\n        \n        return ans;\n    }\n};\n```
['Depth-First Search', 'C', 'Bitmask']