c++ solution
\nclass Solution {\npublic:\n    vector<vector<int>>dp;\n    int find(int node,vector<int>&nums,int &count,int req,int p=-1){\n        int sum=nums[node];\n    
```\nclass Solution {\npublic:\n    vector<vector<int>>dp;\n    int find(int node,vector<int>&nums,int &count,int req,int p=-1){\n        int sum=nums[node];\n        for(auto &it:dp[node]){\n            if(it!=p){\n                sum+=find(it,nums,count,req,node);\n            }\n        }\n        if(sum==req){\n            count++;\n            return 0;\n        }\n        return sum;\n    }\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int n=edges.size();\n        dp=vector<vector<int>>(n+1);\n        for(auto &it:edges){\n            dp[it[0]].push_back(it[1]);\n            dp[it[1]].push_back(it[0]);\n        }\n        int sum=accumulate(nums.begin(),nums.end(),0);\n        int ans=0;\n        for(int i=1;i*i<=sum;i++){\n            if(sum%i==0){\n                int count=0;\n                find(0,nums,count,sum/i);\n                if(count==i){\n                    ans=max(ans,i-1);\n                }\n                count=0;\n                find(0,nums,count,i);\n                if(count==sum/i){\n                    ans=max(ans,sum/i-1);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C', 'C++']