Short C++ DFS Code
\n# Code\n\nclass Solution {\npublic:\n    int max_depth = 0, farthest_node = 0;\n\n    bool isConnected(int &nodes, unordered_map<int, vector<int>> &adj, int c
\n# Code\n```\nclass Solution {\npublic:\n    int max_depth = 0, farthest_node = 0;\n\n    bool isConnected(int &nodes, unordered_map<int, vector<int>> &adj, int curr, int depth  ){\n\n        nodes = nodes^(1<<curr);\n\n        if( depth>max_depth )\n            max_depth = depth, farthest_node = curr;  \n        \n        max_depth = max( max_depth, depth );\n\n        if( !nodes ) return true;\n\n        int ans = false;\n\n        for( auto num: adj[curr] )\n            if( nodes & 1<<num )\n                ans |= isConnected( nodes, adj, num, depth+1 );\n\n        return ans;\n    }\n    \n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        \n        map<int,int> mp; unordered_map<int, vector<int>> adj;\n        \n        for( int i=0; i<edges.size(); i++ ){\n            adj[edges[i][0]].push_back(edges[i][1]);\n            adj[edges[i][1]].push_back(edges[i][0]);\n        }\n  \n        for( int num=1; num<pow(2,n+1); num++ ){\n            if(!(num%2)){\n                int curr = 1, temp = num/2;\n\n                while( !(temp%2) )\n                    curr++, temp /= 2;\n                \n                temp = num, max_depth = 0, farthest_node = curr;\n\n                if(isConnected(temp, adj, curr, 0)){\n                    temp = num;\n                    isConnected(temp, adj, farthest_node, 0);\n                    mp[max_depth]++;\n                }\n            } \n        }\n\n        vector<int> ans(n-1,0);\n        for( int i=1; i<n; i++ )\n            if(mp[i]) ans[i-1]=mp[i];\n\n        return ans;\n    }\n};\n```
['C++']