C++ | DFS
Code\n\nclass Solution {\npublic:\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n            int sum = accumulate(nums.begin(),nums.e
# Code\n```\nclass Solution {\npublic:\n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n            int sum = accumulate(nums.begin(),nums.end(),0);\n            int n = nums.size();\n            vector<int> div;\n            vector<vector<int>> adj(n);\n            for(auto v:edges)\n            {\n                adj[v[0]].push_back(v[1]);\n                adj[v[1]].push_back(v[0]);\n            }\n\n            for(int i=1;i*i <= sum;i++)\n            {\n                if(i*i == sum)\n                {\n                    div.push_back(i);\n                }\n                else if(sum%i == 0)\n                {\n                    div.push_back(i);\n                    div.push_back(sum/i);\n                }\n            }\n\n            int ans = 0;\n            for(int i:div)\n            {\n                pair<int,int> temp = divide(adj,0,-1,i,nums);\n\n                if(temp.first == 0)\n                {\n                ans = max(ans,temp.second - 1);\n                \n                }\n            }\n\n            return ans;\n\n    }\n\n    pair<int,int> divide(vector<vector<int>>& adj,int src,int parent,int val,vector<int>& nums)\n    {\n        \n        int sum = nums[src];\n        int breaks = 0;\n        for(int i:adj[src])\n        {\n            if(i != parent)\n            {\n                pair<int,int> temp = divide(adj,i,src,val,nums);\n                sum += temp.first;\n                breaks += temp.second;\n            }\n        }\n\n        if(sum == val)\n        {\n            return {0,breaks + 1};\n        }\n        else\n        {\n            return {sum,breaks};\n        }\n    }\n};\n```
['Depth-First Search', 'C++']