bitmask + dfs
Intuition\nbitmask + find connected component + tree diameter \n\n# Approach\n\n\n# Complexity\n- Time complexity:O(n * 2^n)\n\n\n- Space complexity:O(n)\n\n# C
# Intuition\nbitmask + find connected component + tree diameter \n\n# Approach\n\n\n# Complexity\n- Time complexity:O(n * 2^n)\n\n\n- Space complexity:O(n)\n\n# Code\n```\n/**\n * @param {number} n\n * @param {number[][]} edges\n * @return {number[]}\n */\nfunction getTreeDiameter(graph, nodes, n) {\n    if (nodes.length === 1) return 0\n    let root = nodes[0]\n    let depths = Array(n).fill(0)\n    let visited = new Set()\n    function dfs(node, depth) {\n        if (visited.has(node)) return\n        visited.add(node)\n        depths[node] = depth\n        for (const to of graph.get(node) || []) {\n            if (!visited.has(to) && nodes.includes(to)) {\n                dfs(to, depth + 1)\n            }\n        }\n    }\n    function dfs2(node) {\n        if (visited.has(node)) return\n        visited.add(node)\n        let dist = 0\n        for (const to of graph.get(node) || []) {\n            if (!visited.has(to) && nodes.includes(to)) {\n                dist = Math.max(dist, dfs2(to) + 1)\n            }\n        }\n        return dist\n    }\n    dfs(root, 0)\n    let maxDepth = -1\n    let furthestNode = -1\n    for (const node of nodes) {\n        if (depths[node] > maxDepth) {\n            maxDepth = depths[node]\n            furthestNode = node\n        }\n    }\n    visited.clear()\n    return dfs2(furthestNode)\n}\nfunction getComponentSize(graph, nodes, n) {\n    let count = 0\n    let visited = Array(n).fill(false)\n    function dfs(node) {\n        if (visited[node]) return\n        visited[node] = true\n        for (const to of graph.get(node) || []) {\n            if (!visited[to] && nodes.includes(to)) {\n                dfs(to)\n            }\n        }\n    }\n    for (const node of nodes) {\n        if (!visited[node]) {\n            dfs(node)\n            count += 1\n        }\n    }\n    return count\n}\nvar countSubgraphsForEachDiameter = function (n, edges) {\n    let graph = new Map()\n    let distMap = new Map()\n    for (const [a, b] of edges) {\n        u = a - 1\n        v = b - 1\n        if (!graph.has(u)) {\n            graph.set(u, [])\n        }\n        if (!graph.has(v)) {\n            graph.set(v, [])\n        }\n        graph.get(u).push(v)\n        graph.get(v).push(u)\n    }\n    for (let mask = 0; mask < (1 << n); mask++) {\n        let select = []\n        for (let j = 0; j < n; j++) {\n            if (mask & (1 << j)) {\n                select.push(j)\n            }\n        }\n        let size = getComponentSize(graph, select, n)\n        if (size === 1) {\n            let dist = getTreeDiameter(graph, select, n)\n            if (!distMap.has(dist)) {\n                distMap.set(dist, [])\n            }\n            distMap.get(dist).push(select)\n        }\n    }\n    let ans = Array(n - 1).fill(0)\n    for (let d = 1; d <= n - 1; d++) {\n        let count = 0\n        if (distMap.has(d)) {\n            count = distMap.get(d).length\n        }\n        ans[d - 1] = count\n    }\n    return ans\n};\n```
['JavaScript']