C++ && Bitmask && DFS Solution
Intuition\nSince the constraints are pretty small (n<=15) we can surely think of bitmask.\n# Approach\nTry all possible subtrees using bitmask.\ncheck if it a v
# Intuition\nSince the constraints are pretty small (n<=15) we can surely think of bitmask.\n# Approach\nTry all possible subtrees using bitmask.\ncheck if it a valid subtree.\nthen find maxDis for each valid subtree.\n\n\n# Code\n```\nclass Solution {\npublic:\n    void dfs(int curr, int prev, int d, vector<int> adj[], int &maxinode, int &ans){\n        if (d > ans){\n            ans = d;\n            maxinode = curr;\n        }\n        for (auto i : adj[curr]){\n            if (i != prev){\n                dfs(i, curr, d + 1, adj, maxinode, ans);\n            }\n        }\n    }\n    void dfs(int node,int prev,vector<int>adj2[],int &mask2){\n        mask2=(mask2|(1<<(node-1)));\n        for(auto it:adj2[node]){\n            if(it!=prev){\n                dfs(it,node,adj2,mask2);\n            }\n        }\n    }\n    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {\n        vector<int>adj[n+1];\n        for(auto it:edges){\n            adj[it[0]].push_back(it[1]);\n            adj[it[1]].push_back(it[0]);\n        }\n        vector<int>res(n-1);\n        for(int i=3;i<=(1<<n);i++){\n            int mask=i;\n            vector<int>adj2[n+1];\n            int start=1;\n            for(auto it:edges){\n                int u=it[0];\n                int v=it[1];\n                if((mask&(1<<(u-1)))&&(mask&(1<<(v-1)))){\n                    start=u;\n                    adj2[u].push_back(v);\n                    adj2[v].push_back(u);\n                }\n            }\n            int mask2=0;\n            dfs(start,-1,adj2,mask2);\n            if(mask2==mask){\n                int ans = 0;\n                int maxinode = 0;\n                \n                dfs(start, -1, 0, adj2, maxinode, ans);\n                dfs(maxinode, -1, 0, adj2, maxinode, ans);\n                res[ans-1]++;\n            }\n        }\n        return res;\n    }\n};\n```
['C++']