Short DFS Code C++
Code\n\nclass Solution {\npublic:\n\n    int dfs( int node, int parent, vector<int>& nums, vector<vector<int>>& adj, int &components, int sum ){\n        int va
# Code\n```\nclass Solution {\npublic:\n\n    int dfs( int node, int parent, vector<int>& nums, vector<vector<int>>& adj, int &components, int sum ){\n        int val = nums[node];\n        for( int nextNode: adj[node] ){\n            if( nextNode==parent ) continue;\n            val += dfs( nextNode, node, nums, adj, components, sum );\n        }\n\n        if( val == sum ){ \n            components--;\n            return 0;\n        }\n        return val;\n    }\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        \n        int n = nums.size(), sum = accumulate(nums.begin(),nums.end(),0), ans = 0, maxi = *max_element(nums.begin(), nums.end());;\n\n        vector<vector<int>> adj(n);\n        \n        for( auto v: edges ){\n            adj[v[0]].push_back(v[1]);\n            adj[v[1]].push_back(v[0]);\n        }\n\n        \n        for( int i=sum; i>=maxi; i-- )\n            if( sum%i==0 ){\n                int components = sum/i;\n                dfs( 0, -1, nums, adj, components, i );\n                if(components==0) ans = max(ans, (sum/i) - 1);\n            }\n\n        return ans;\n    }\n};\n```
['C++']