1617. Count Subtrees With Max Distance Between Cities
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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n---\n\n```\nvar countSubgraphsForEachDiameter = function (n, edges) {\n  const ad = Array.from({ length: n }, () => new Set());\n  for (const [u, v] of edges) {\n    ad[u - 1].add(v - 1);\n    ad[v - 1].add(u - 1);\n  }\n  function getDepth_fromOneNodeToAllNodesInSubtree(U) {\n    function dfs(u, visited) {\n      if (!U.has(u)) return -Infinity;\n      if (visited.has(u)) return -Infinity;\n      visited.add(u);\n      let d = 0;\n      for (const v of ad[u]) {\n        d = Math.max(d, 1 + dfs(v, visited));\n      }\n      return d;\n    }\n    if (!U.size) return -1;\n    let max = -1;\n    for (const u of U) {\n      const visited = new Set();\n      const d = dfs(u, visited);\n      if (visited.size !== U.size) continue;\n      max = Math.max(max, d);\n    }\n    return max;\n  }\n  const ans = Array(n - 1).fill(0);\n  for (let i = 0; i < 1 << n; i++) {\n    const subtree = new Set();\n    for (let j = 0; j < n; j++) {\n      if (i & (1 << j)) subtree.add(j);\n    }\n    const maxDepth = getDepth_fromOneNodeToAllNodesInSubtree(subtree);\n    if (maxDepth > 0) ans[maxDepth - 1]++;\n  }\n  return ans;\n};\n```\n\n---\n
['JavaScript']