C++ Simple DP Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nvoid dfs(int curr_node,vector<int>edg[],int par,vector<int>&sub_sum,vector<int>&nums){\n    sub_sum[curr_node]=nums[curr_node];\n    for(int i=0;i<edg[curr_node].size();i++){\n        if(edg[curr_node][i]!=par){\n            dfs(edg[curr_node][i],edg,curr_node,sub_sum,nums);\n            sub_sum[curr_node]=sub_sum[curr_node]+sub_sum[edg[curr_node][i]];\n        }\n    }\n}\nint dfs2(int curr_node,vector<int>edg[],int par,vector<int>&sub_sum,int &s){\n    int curr=sub_sum[curr_node];\n    int cmp=0;\n    if(curr==s){\n        return 1;\n    }\n    for(int i=0;i<edg[curr_node].size();i++){\n        if(edg[curr_node][i]!=par){\n            cmp=cmp+dfs2(edg[curr_node][i],edg,curr_node,sub_sum,s);\n        }\n    }\n    curr=sub_sum[curr_node]-cmp*s;\n    if(curr==s){\n        cmp=cmp+1;\n        return cmp;\n    }\n    return cmp;\n}\n\n\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        if(edges.size()==0)return 0;\n        vector<int>sub_sum(nums.size(),0);\n        int root=0;\n        vector<int>edg[nums.size()];\n        for(int i=0;i<edges.size();i++){\n            int nd1=edges[i][0];\n            int nd2=edges[i][1];\n            edg[nd1].push_back(nd2);\n            edg[nd2].push_back(nd1);\n        }\n        dfs(0,edg,-1,sub_sum,nums);\n        int s=0;\n        for(int i=0;i<nums.size();i++)s=s+nums[i];\n        int ans=0;\n        for(int i=1;i<=nums.size();i++){\n            if(s%i==0){\n            int curr=s/i;\n            int hp2=dfs2(0,edg,-1,sub_sum,curr);\n            if(hp2==i)ans=max(ans,hp2);\n            }\n        }\n        ans=ans-1;\n      return ans;\n    }\n};\n```
['Array', 'Dynamic Programming', 'Tree', 'C++']