[Java] Powerset solution + find max distance
\nclass Solution {\n    public int[] countSubgraphsForEachDiameter(int n, int[][] edges) {\n        List<List<Integer>> graph = new ArrayList<>();\n        for 
```\nclass Solution {\n    public int[] countSubgraphsForEachDiameter(int n, int[][] edges) {\n        List<List<Integer>> graph = new ArrayList<>();\n        for (int i = 0; i < n; i++) {\n            graph.add(new ArrayList<>());\n        }\n        \n        for (int[] edge : edges) {\n            graph.get(edge[0] - 1).add(edge[1] - 1);\n            graph.get(edge[1] - 1).add(edge[0] - 1);\n        }\n        \n        int[] answer = new int[n - 1];\n        \n        for (int bits = 1; bits < (1 << n); bits++) {\n            Set<Integer> nodes = new HashSet<>();\n            \n            for (int j = 0; j < n; j++) {\n                if (((1 << j) & bits) != 0) nodes.add(j);\n            }\n            \n            if (isSubtree(graph, nodes)) {\n                answer[maxDistance(graph, nodes) - 1]++;\n            }\n        }\n        \n        return answer;\n    }\n    \n    private boolean isSubtree(List<List<Integer>> edges, Set<Integer> nodes) {\n        if (nodes.size() < 2) {\n            return false;\n        }\n        \n        Queue<Integer> queue = new LinkedList<>();\n        Set<Integer> visited = new HashSet<>();\n        queue.add(nodes.iterator().next());\n        visited.add(queue.peek());\n        \n        while (!queue.isEmpty()) {\n            int cur = queue.poll();\n            \n            for (int edge : edges.get(cur)) {\n                if (!visited.contains(edge) && nodes.contains(edge)) {\n                    visited.add(edge);\n                    queue.add(edge);\n                }\n            }\n        }\n        \n        return visited.size() == nodes.size();\n    }\n    \n    private int maxDistance(List<List<Integer>> edges, Set<Integer> nodes) {\n        int deepest = findFarthestNode(edges, nodes, nodes.iterator().next(), -1).getKey();\n        return findFarthestNode(edges, nodes, deepest, -1).getValue();\n    }\n    \n    private Pair<Integer, Integer> findFarthestNode(List<List<Integer>> edges, Set<Integer> nodes, int cur, int from) {\n        Pair<Integer, Integer> answer = new Pair(cur, 0);\n        \n        for (int edge : edges.get(cur)) {\n            if (edge == from || !nodes.contains(edge)) {\n                continue;\n            }\n            \n            Pair<Integer, Integer> other = findFarthestNode(edges, nodes, edge, cur);\n            if (other.getValue() + 1 > answer.getValue()) {\n                answer = new Pair(other.getKey(), other.getValue() + 1);\n            }\n        }\n        \n        return answer;\n    }\n}\n```
[]