<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<vector<int>> g;

  // DFS function to compute the maximum depth and update the diameter
   int dfsDepth(int v, int i , unordered_set<int> &vis, int &res){
        vis.insert(v);
        int max1=0;
        int max2=0;
        for(auto nei: g[v]){
            if(((1<<nei)&i) && vis.find(nei)==vis.end()){
                int depth=1+dfsDepth(nei,i,vis,res);
                if(depth>max1){
                    max2=max1;  
                    max1=depth;
                }
                //taking two max vaues and resturng the max out of thm
                else if(depth>max2){
                    max2=depth;
                }
            }
        }
        res=max(res,max1+max2);
        return max1;
    }
int f(int i, int startNode) {
    int diameter = 0;
    // Calculate maximum depth and update diameter from startNode
    unordered_set<int> subtree;
    dfsDepth( startNode,i, subtree,diameter);
    return diameter;
}

    // Function to find the first set bit in the binary representation of i
    int frstSetBit(int i, int n) {
        for (int j = 0; j < n; j++) {
            if ((1 << j) & i) { // if bit j is set
                return j;
            }
        }
        return -1; // No set bit found
    }

bool validSubtree(int i, int n, int firstSetBit, int& mask) {

    mask|=(1<<firstSetBit);
        if (mask == i )
        return true;

    for (auto p : g[firstSetBit]) {
        if ((i & (1 << p)) && !(mask & (1 << p))) { // twon thinf tocheck , valid tree part,and prev visisted
            if(validSubtree(i, n, p,mask))
            return true;
        }
    }
    return false;
}   



    vector<int> countSubgraphsForEachDiameter(int n, std::vector<std::vector<int>>& edges) {
        g.resize(n);
        std::vector<int> ans(n-1, 0);

        // Build the graph
        for (auto p : edges) {
            g[p[0] - 1].push_back(p[1] - 1);
            g[p[1] - 1].push_back(p[0] - 1);
        }

        // Iterate over all possible subtrees represented by bitmasks
        for (int i = 1; i < (1 << n); i++) { int firstSetBit = frstSetBit(i, n);
int o=0;
        if   (validSubtree(i, n,firstSetBit,o))// if this is a valid subtree, then find the largest diameter of this tree
               {
                int diameter = f(i, firstSetBit);
                if (diameter > 0 && diameter < n) {
                    ans[diameter - 1]++;
                }
            }
        }

        return ans;
    }
};
22728