Very Hard
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```\nclass Solution {\n    private List<Integer>[] g;\n    private int msk;\n    private int nxt;\n    private int mx;\n\n    public int[] countSubgraphsForEachDiameter(int n, int[][] edges) {\n        g = new List[n];\n        Arrays.setAll(g, k -> new ArrayList<>());\n        for (int[] e : edges) {\n            int u = e[0] - 1, v = e[1] - 1;\n            g[u].add(v);\n            g[v].add(u);\n        }\n        int[] ans = new int[n - 1];\n        for (int mask = 1; mask < 1 << n; ++mask) {\n            if ((mask & (mask - 1)) == 0) {\n                continue;\n            }\n            msk = mask;\n            mx = 0;\n            int cur = 31 - Integer.numberOfLeadingZeros(msk);\n            dfs(cur, 0);\n            if (msk == 0) {\n                msk = mask;\n                mx = 0;\n                dfs(nxt, 0);\n                ++ans[mx - 1];\n            }\n        }\n        return ans;\n    }\n\n    private void dfs(int u, int d) {\n        msk ^= 1 << u;\n        if (mx < d) {\n            mx = d;\n            nxt = u;\n        }\n        for (int v : g[u]) {\n            if ((msk >> v & 1) == 1) {\n                dfs(v, d + 1);\n            }\n        }\n    }\n}\n```
['Java']