C++| 4 easy Steps | brute-force
At first the glance, this problem looks complex and coming up with an optimal approach seems difficult but, we actually do not need to think that deep. Since co
At first the glance, this problem looks complex and coming up with an optimal approach seems difficult but, we actually do not need to think that deep. Since constraint is vey small, we can try the brute force. The solution is divide into 4 steps:\n1. Firstly build a graph using `edges`\n1. Generate all possible combination of nodes using **bitmask**\n1. Now check if all those nodes are connected or not using **DSU**, if not connected simply return. I have used **DSU**, you can simply use DFS or any other tool\n1. find the **diameter of subtree** from any node ( similar to [Diameter of Binary Tree](http://leetcode.com/problems/diameter-of-binary-tree/description/))\n\nupvote if it helps\n```\nclass Solution {\npublic:\n    int mp[15] = {};\n    int find(vector<int> &nums,int i){\n        if(nums[i]==-1) return i;\n        else return nums[i] = find(nums,nums[i]);\n    }\n    void union_(vector<int> &nums,int x,int y){\n        int i = find(nums,x), j=find(nums,y);\n        if(i!=j){\n            if(i<j) nums[j] = i;\n            else nums[i] = j;\n        }\n    }\n    int findmaxdist(vector<vector<int>> &nums,vector<bool> &arr,int vis,int in, int &sum){ // to find diameter of subtree\n        arr[in] = 1; int re = 0;\n        priority_queue<int,vector<int>,greater<int>> q;\n        for(int i = 0; i<nums[in].size();++i){\n            int j = nums[in][i];\n            if(bool(vis&(1<<j)) && arr[j]==0){\n                int k = findmaxdist(nums,arr,vis,j,sum);  re=max(re,k);\n                q.push(k);  if(q.size()>2) q.pop();\n            }\n        }\n        int p = 0;\n        while(!q.empty()){  p+=q.top(); q.pop(); }\n\t\t\n        sum=max(sum,p);\n        return re+1;\n    }\n    void count(vector<vector<int>> g,int vis,int n){\n        vector<int> nums(n,-1); int l = 0;\n        for(int i = 0; i<n;++i){\n            if((vis&(1<<i))){\n                l=i;\n                for(int j = 0; j<g[i].size();++j){\n                    int k = g[i][j];\n                    if((vis&(1<<k))) union_(nums,i,k);\n                }\n            }\n        }\n        int re = 0;\n        for(int i = 0; i<n;++i){\n            if((vis&(1<<i))) if(nums[i]==-1) re++;\n        }\n        if(re!=1) return;\n        vector<bool> arr(n,0);\n        int k = 0;\n        findmaxdist(g,arr,vis,l,k);\n        mp[k]++;\n    }\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& arr) {\n        vector<vector<int>> nums(n);\n        for(int i = 0; i<arr.size();++i){\n            nums[arr[i][0]-1].push_back(arr[i][1]-1);\n            nums[arr[i][1]-1].push_back(arr[i][0]-1);\n        }\n        int last = (1<<n);\n        for(int i = 1; i<last;++i) count(nums,i,n);\n        vector<int> re;\n        for(int i = 1; i<n;++i) re.push_back(mp[i]);\n        return re;\n    }\n};\n```
['Graph', 'C']