<p>There are <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are given an array <code>edges</code> of size <code>n-1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents a bidirectional edge between cities <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. There exists a unique path between each pair of cities. In other words, the cities form a <strong>tree</strong>.</p>

<p>A <strong>subtree</strong> is a subset of cities where every city is reachable from every other city in the subset, where the path between each pair passes through only the cities from the subset. Two subtrees are different if there is a city in one subtree that is not present in the other.</p>

<p>For each <code>d</code> from <code>1</code> to <code>n-1</code>, find the number of subtrees in which the <strong>maximum distance</strong> between any two cities in the subtree is equal to <code>d</code>.</p>

<p>Return <em>an array of size</em> <code>n-1</code> <em>where the </em><code>d<sup>th</sup></code><em> </em><em>element <strong>(1-indexed)</strong> is the number of subtrees in which the <strong>maximum distance</strong> between any two cities is equal to </em><code>d</code>.</p>

<p><strong>Notice</strong>&nbsp;that&nbsp;the <strong>distance</strong> between the two cities is the number of edges in the path between them.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/p1.png" style="width: 161px; height: 181px;" /></strong></p>

<pre>
<strong>Input:</strong> n = 4, edges = [[1,2],[2,3],[2,4]]
<strong>Output:</strong> [3,4,0]
<strong>Explanation:
</strong>The subtrees with subsets {1,2}, {2,3} and {2,4} have a max distance of 1.
The subtrees with subsets {1,2,3}, {1,2,4}, {2,3,4} and {1,2,3,4} have a max distance of 2.
No subtree has two nodes where the max distance between them is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 2, edges = [[1,2]]
<strong>Output:</strong> [1]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 3, edges = [[1,2],[2,3]]
<strong>Output:</strong> [2,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 15</code></li>
	<li><code>edges.length == n-1</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li>
	<li>All pairs <code>(u<sub>i</sub>, v<sub>i</sub>)</code> are distinct.</li>
</ul>
class Solution {
public:
    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {
        // Step 1: Graph representation using adjacency list
        vector<vector<int>> graph(n + 1); // 1-indexed graph
        
        // Building the graph from edges
        for (const auto& edge : edges) {
            int u = edge[0], v = edge[1];
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        
        // Step 2: Calculate distances between every pair of cities
        vector<vector<int>> dist(n + 1, vector<int>(n + 1, -1)); // Distance array
        
        // BFS function to calculate distances from 'start' node
        auto bfs = [&](int start) {
            vector<int> distance(n + 1, -1);
            queue<int> q;
            q.push(start);
            distance[start] = 0;
            
            while (!q.empty()) {
                int u = q.front(); q.pop();
                for (int v : graph[u]) {
                    if (distance[v] == -1) {  // unvisited
                        distance[v] = distance[u] + 1;
                        q.push(v);
                    }
                }
            }
            return distance;
        };
        
        // Fill the dist array with distances between all pairs
        for (int i = 1; i <= n; ++i) {
            vector<int> distances_from_i = bfs(i);
            for (int j = 1; j <= n; ++j) {
                dist[i][j] = distances_from_i[j];
            }
        }
        
        // Step 3: Iterate over all subsets of nodes (represented as bitmasks)
        vector<int> result(n - 1, 0); // Result array to store counts for diameters 1 to n-1
        
        for (int mask = 1; mask < (1 << n); ++mask) {
            // Step 4: Collect the nodes in the current subset
            vector<int> nodes_in_subset;
            for (int i = 0; i < n; ++i) {
                if (mask & (1 << i)) {
                    nodes_in_subset.push_back(i + 1);
                }
            }
            
            // Step 5: Check if the current subset forms a valid connected subtree
            if (isValidSubtree(nodes_in_subset, graph)) {
                // Step 6: Calculate the maximum distance in this subtree
                int max_distance = 0;
                for (int i = 0; i < nodes_in_subset.size(); ++i) {
                    for (int j = i + 1; j < nodes_in_subset.size(); ++j) {
                        max_distance = max(max_distance, dist[nodes_in_subset[i]][nodes_in_subset[j]]);
                    }
                }
                
                // Step 7: Update the result for the corresponding diameter
                if (max_distance > 0 && max_distance < n) {
                    result[max_distance - 1]++;
                }
            }
        }
        
        return result;
    }

private:
    // Helper function to check if a set of nodes forms a valid connected subtree
    bool isValidSubtree(const vector<int>& nodes, const vector<vector<int>>& graph) {
        if (nodes.empty()) return false;

        queue<int> q;
        unordered_set<int> visited;
        
        q.push(nodes[0]);
        visited.insert(nodes[0]);
        
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int v : graph[u]) {
                if (visited.find(v) == visited.end() && 
                    find(nodes.begin(), nodes.end(), v) != nodes.end()) {
                    visited.insert(v);
                    q.push(v);
                }
            }
        }
        
        return visited.size() == nodes.size();  // Valid subtree if all nodes are visited
    }
};

412453