c++||simple||queue||bfs
\nclass Solution {\npublic:\n    \n    bool solve_bfs(int sum,vector<int>nums,vector<vector<int>>&adj,vector<int>cnt,queue<int>q)\n    {\n        while(!q.empty
```\nclass Solution {\npublic:\n    \n    bool solve_bfs(int sum,vector<int>nums,vector<vector<int>>&adj,vector<int>cnt,queue<int>q)\n    {\n        while(!q.empty())\n        {\n            queue<int>q1;\n            while(!q.empty())\n            {\n                int per=q.front();\n                q.pop();\n                if(nums[per]>sum)\n                    return 0;\n                \n                for(auto it:adj[per])\n                {\n                    if(nums[per]!=sum)   //if sum is not equal to nums[per]\n                    nums[it]+=nums[per]; \n                    \n                    cnt[it]--;\n                    if(cnt[it]==1)\n                    {\n                        q1.push(it);\n                    }\n                }\n                \n            }\n           q=q1;\n        }\n        return 1;\n    }\n    \n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int sum=0;\n        int n=nums.size();\n        for(int i=0;i<nums.size();i++)\n        {\n            sum+=nums[i];\n        }\n        \n        vector<vector<int>>adj(n);\n        for(int i=0;i<edges.size();i++)\n        {\n            adj[edges[i][0]].push_back(edges[i][1]);\n            adj[edges[i][1]].push_back(edges[i][0]);\n        }\n        \n        vector<int>cnt(n,0);\n        queue<int>q;\n        for(int i=0;i<n;i++)\n        {\n            cnt[i]=adj[i].size();\n            if(cnt[i]==1) //leaf node or root node\n            {\n               q.push(i);\n            }\n        }\n        \n         \n        for(int i=n;i>=2;i--)\n        {\n            if(sum%i==0)\n            { \n                if(solve_bfs(sum/i,nums,adj,cnt,q))\n                    return i-1;\n            }\n        }\n        \n        return 0;\n        \n    }\n};\n```
[]