DFS | O(N * no.of factors(sum of vals) ) | C++
\nclass Solution {\npublic:\n    \n    int cnt;\n    bool flag;\n    \n    vector<int> Cal(int num){\n        int sq = sqrt(num)+1;\n        vector<int> fact;\n
```\nclass Solution {\npublic:\n    \n    int cnt;\n    bool flag;\n    \n    vector<int> Cal(int num){\n        int sq = sqrt(num)+1;\n        vector<int> fact;\n        for(int i=2;i<=sq;i++){\n            if(num%i==0){\n                int j = num/i;\n                fact.push_back(i);\n                if(i!=j) fact.push_back(j);\n            }\n        }\n        fact.push_back(1);\n        sort(fact.begin(),fact.end());\n        return fact;\n    }\n    \n    int Check(vector<vector<int>>& Graph, int node, int f, int par, vector<int>& val){\n        if(flag) return -1;\n        int sum = val[node];\n        for(int next : Graph[node]){\n            if(next == par) continue;\n            int x =  Check(Graph, next, f, node, val);\n            if(x==-1) return -1;\n            sum += x;\n        }\n     \n        if(sum==f){\n            cnt++;\n            return 0;\n        }else if(sum < f){\n            return sum;\n        }\n        flag = true;\n        return -1;\n        \n    }\n    \n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int N = nums.size();\n        if(N==1) return 0;\n        int sum = 0;\n        for(int i=0;i<N;i++) sum += nums[i];\n        \n        vector<vector<int>> Graph(N);\n        for(auto edge : edges){\n            int u = edge[0];\n            int v = edge[1];\n            Graph[u].push_back(v);\n            Graph[v].push_back(u);\n        }\n        \n        vector<int> fact = Cal(sum);\n        int Ans = 0;\n        for(int f : fact){\n            cnt = 0;\n            flag = false;\n            Check(Graph, 0, f, -1, nums);\n            if(!flag){\n                Ans = max(Ans,cnt-1);\n            } \n        }\n        return Ans;\n    }\n};\n```
['Tree', 'Depth-First Search', 'Recursion']