Brute force - Tree diameter with DFS
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n.2^n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nint nodecnt;\nint depth[15];\nvoid dfs(int i, int par, int dep, vector<vector<int>> &g, int nodes) {\n    if (!((nodes>>i)&1)) return;\n    depth[i]=dep;\n    nodecnt++;\n    for (auto &v:g[i]) if (v!=par)\n        dfs(v,i,dep+1,g,nodes);\n}\n\nclass Solution {\npublic:\n//O(n.2^n)\nvector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n    vector<int> ans(n-1,0);\n    vector<vector<int>> g;g.resize(n);\n    for (auto &ed:edges) { g[ed[0]-1].push_back(ed[1]-1); g[ed[1]-1].push_back(ed[0]-1); }\n    for (int mask=0;mask<(1<<n);mask++) {\n        // for (auto &_:nodes)cout<<_<<\' \';cout<<endl;\n        int tot=__builtin_popcount(mask);\n        if (!tot) continue;\n        nodecnt=0;\n        memset(depth,-1,sizeof(depth));\n\n        int st=0;\n        while (!((mask>>st)&1)) st++;\n\n        dfs(st,-1,0,g,mask);\n        if (nodecnt==tot) { //connected\n            // for (auto &_:nodes)cout<<_+1<<\' \';cout<<"\\t\\t";\n            // for (int i=0;i<n;i++) cout<<depth[i]<<\' \';cout<<"\\t";\n            int max_dep_node=0;\n            for (int i=1;i<n;i++) if (depth[i]>depth[max_dep_node])\n                max_dep_node=i;\n            // cout<<max_dep_node<<endl;\n            // assert(nodes.find(max_dep_node)!=nodes.end());\n\n            memset(depth,-1,sizeof(depth));\n            dfs(max_dep_node,-1,0,g,mask);\n            max_dep_node=0;\n            for (int i=1;i<n;i++) if (depth[i]>depth[max_dep_node])\n                max_dep_node=i;\n\n            // cout<<depth[max_dep_node]<<endl;\n\n            if (depth[max_dep_node]) ans[depth[max_dep_node]-1]++;\n        }\n    }\n    return ans;\n}\n};\n```
['C++']