[C++] O(2^N*N) BitMask + DFS
\t\n\tclass Solution {\n\tpublic:\n\t\tint mx, mxp, cnt; // diameter, farthest point, graph node count\n\t\tvector g[17];\n\t\tvoid dfs(int u, int d, int p){ //
\t\n\tclass Solution {\n\tpublic:\n\t\tint mx, mxp, cnt; // diameter, farthest point, graph node count\n\t\tvector<int> g[17];\n\t\tvoid dfs(int u, int d, int p){ // current node, depth, parent node\n\t\t\tcnt++;\n\t\t\tif(d > mx){\n\t\t\t\tmx = d;\n\t\t\t\tmxp = u;\n\t\t\t}\n\t\t\tfor(int v: g[u]){\n\t\t\t\tif(v != p){\n\t\t\t\t\tdfs(v, d+1, u);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n\t\t\tvector<int> res(n-1);\n\t\t\tfor(int i = 1; i < (1<<n); ++i){ // enum all subset\n\t\t\t\tif(__builtin_popcount(i) == 1) continue;\n\t\t\t\tfor(int j = 0; j <= n; ++j) g[j].clear();\n\n\t\t\t\tint pt = -1; // random pick a point in graph as a start point\n\t\t\t\tfor(auto &e: edges){\n\t\t\t\t\tint u = e[0]-1, v = e[1]-1;\n\t\t\t\t\tif( ((1<<u)&i) && ((1<<v)&i) ){ // make sure the edge point must be in the mask\n\t\t\t\t\t\tg[u].emplace_back(v);\n\t\t\t\t\t\tg[v].emplace_back(u);\n\t\t\t\t\t\tif(pt == -1) pt = u;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif(pt == -1) continue;\n\t\t\t\t\n\t\t\t\t/**\n\t\t\t\t\tTwo dfs, one is from a start point find the farthest point.\n\t\t\t\t\tSecond, from that farthest point. Again find the other farthest point.\n\t\t\t\t\tFinally, distance is diameter.\n\t\t\t\t**/\n\t\t\t\t\n\t\t\t\tmx = 0, mxp = -1, cnt = 0;\n\t\t\t\tdfs(pt, 0, -1);\n\t\t\t\tmx = 0, cnt = 0;\n\t\t\t\tdfs(mxp, 0, -1);\n\t\t\t\tif(cnt == __builtin_popcount(i) && mx > 0){\n\t\t\t\t\tres[mx-1]++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn res;\n\t\t}\n\t};
['Depth-First Search', 'C']