Bitmask + DFS + Recursion
Intuition\n Describe your first thoughts on how to solve this problem. \nYou have to build all possible trees and then do a dfs on every tree to find the diamet
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nYou have to build all possible trees and then do a dfs on every tree to find the diameter of the tree.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI took a bitmask for the allowed variable, if ith bit is set it means my current tree has ith node, now i run a simple dfs by visiting only the allowed nodes and caluclating the diameter of the tree thereby \nAlso the visited bitmask  has been used to maintain that infinite loop is not created, because the edges are bidirectional hence going back to the parent is not encouraged.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(2^n * n * n) which makes it ~ 32000 * 225 ~ 1e5\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n*n);\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> adj;\n    int dfs(int node,int allowed,int& ans,int& vis){\n        if(!(allowed & (1<<(node)))){\n            for(int j=1;j<16;j++) if(allowed & (1<<(j))) {node=j; break;}\n        }\n        int depth1=0,depth2=0;\n        // vis[node]=1;\n        vis=(vis | (1<<(node)));\n        for(auto x: adj[node]){\n            if((allowed & (1<<(x))) && !(vis & (1<<(x)))){\n                int xx=dfs(x,allowed,ans,vis);\n                if(xx>depth1) {\n                    depth2=depth1;\n                    depth1=xx;\n                }\n                else if(xx>depth2) depth2=xx;\n            }\n        }\n        ans=max(ans,depth1+depth2);\n        return max(depth1,depth2)+1;\n    }\n    bool check(int allow){\n        int allowed=allow;\n        int root=-1;\n        for(int i=1;i<16;i++) if(allowed & (1<<i)) root=i;\n        if(root==-1) return false;\n        stack<int> st;\n        st.push(root);\n        while(!st.empty()){\n            int node=st.top();\n            st.pop();\n            allowed=(allowed & ~(1<<(node)));\n            for(auto x: adj[node]) \n            if(allowed & (1<<x)) st.push(x);\n        }\n        for(int i=0;i<16;i++) if(allowed & (1<<i)) return 0;\n        return 1;\n    }\n    void solve(int i,int n,int allowed,vector<int>& res){\n        if(i>n) {\n            int ans=0;\n            if(check(allowed)){\n            // vector<int> vis(n+1,0);\n            int vis=0;\n            dfs(1,allowed,ans,vis);\n            res[ans]++;}\n            return;\n        }\n        solve(i+1,n,allowed,res);\n        allowed+=(1<<i);\n        solve(i+1,n,allowed,res);\n    }\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        adj.resize(n+1);\n        for(auto edge: edges) \n        {\n        adj[edge[0]].push_back(edge[1]);\n        adj[edge[1]].push_back(edge[0]);\n        }\n        // vector<int> allowed(n+1,0);\n        int allowed=0;\n        vector<int> res(n,0);\n        solve(1,n,allowed,res);\n        vector<int> finalres;\n        for(int i=1;i<n;i++)\n        finalres.push_back(res[i]);\n        return finalres;\n    }\n}; \n```
['C++']