[C++] Solution with Very clear code, with sperate functions
\n# Approach\n Describe your approach to solving the problem. \n- create a graph to map all the edges\n- generate all subtrees, use bitmask to save memory.\n- n
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- create a graph to map all the edges\n- generate all subtrees, use bitmask to save memory.\n- now inside the `helper` function decode the subtrees one by one and then call `findMaxDist` function to find the max distance for that subtree and also check if that subtree is valid or i.e. the path from one city to another should go though the cities within that subtree.\n- and find the max distance for that subtree and store the result in `maxi` variable\n- `BFS` function finds the minimum distance to reach from one node to other and if `temp` is marked `-1`, it means that we cannot push that node to our queue. In the end if `temp` contains any value equal to `10^6`, it suggests that we were not able to visit that node and that particular subtree was invalid.\n# Complexity\n- Time complexity: O(2^n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(2^n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    //function to create adjacency list using given edges, this will be an undirected graph\n    void createGraph(vector<vector<int>> &graph, vector<vector<int>> &edges){\n        for(int i=0; i<edges.size(); i++){\n            graph[edges[i][0]].push_back(edges[i][1]);\n            graph[edges[i][1]].push_back(edges[i][0]);\n        }\n    }\n    \n    //function to find the minimum distance to travel all the nodes from the current city\n    vector<int> bfs(vector<vector<int>> &graph, int city, vector<int> &temp){\n        int n = graph.size();\n        temp[city] = 0;\n        queue<pair<int,int>> q;       //city, distance\n        q.push({city, 0});      \n\n        while(!q.empty()){\n            int newCity= q.front().first;\n            int dist = q.front().second;\n            q.pop();\n\n            for(int i=0; i<graph[newCity].size(); i++){\n                int travel = graph[newCity][i];\n                if(temp[travel] > dist+1){\n                    temp[travel] = dist+1;\n                    q.push({travel, dist+1});\n                }\n                if(temp[travel] == -1) continue;\n            }\n        }\n        return temp;\n    }\n\n    \n\n    //function to generate all subtrees or subsets. This function uses mask integer to store all subtrees\n    void generateSubtrees(int mask, vector<int> &subtrees, int ind, int n){\n        if(ind > n){\n            subtrees.push_back(mask);\n            return;\n        }\n        generateSubtrees(mask|(1<<ind), subtrees, ind+1, n);\n        generateSubtrees(mask, subtrees, ind+1, n);\n    }\n\n\n    //function to find the maximum distance to travel from one city to another within a subtree\n    int findMaxDist(vector<vector<int>> &graph, vector<int> &subtree){\n        int maxi = 0;\n\n        for(int i=0; i<subtree.size(); i++){\n            vector<int> temp(graph.size(), -1);\n            for(int i=0; i<subtree.size(); i++){\n                temp[subtree[i]] = 1e6;\n            }\n            vector<int> dist = bfs(graph, subtree[i], temp);\n            for(int j=0; j<dist.size(); j++){\n                if(dist[j] == 1e6) return 0;\n                maxi = max(maxi, dist[j]);\n            }\n        }\n        \n        return maxi;\n    }\n    \n//function to convert mask to a vector\n    vector<int> decodeSubtree(int mask){\n        vector<int> subtree;\n        int i=0; \n        while(mask>0){\n            if((mask&1) == 1){\n                subtree.push_back(i);\n            }\n            mask = mask>>1;\n            i++;\n        }\n        return subtree;\n    }\n\n    //function to count required subtrees\n    void helper(vector<vector<int>> &graph, vector<int> &ans, vector<int> &subtrees, int n){\n        //store maximum distance for every subtree in the end of the subset\n        for(int i=0; i<subtrees.size(); i++){\n            vector<int> subtree = decodeSubtree(subtrees[i]);\n            if(subtree.size()<2) continue;\n            int dist = findMaxDist(graph, subtree);\n            if(dist>=1 && dist<=n) ans[dist-1]++;\n        }\n    }\n\n\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<vector<int>> graph(n+1);\n        vector<int> ans(n-1, 0);\n\n        //create the graph using the given edges\n        createGraph(graph, edges);\n\n        //find all substrees of trees\n        int mask = 0;\n        vector<int> subtrees;\n        generateSubtrees(mask, subtrees, 1, n);\n\n        //function to find the final answer\n        helper(graph, ans, subtrees, n);\n        \n\n        return ans;\n    }\n};\n```
['C++']