Clear & short Explanation - DFS - O( sqrt(sum) * N ) - comments added
\n# Approach\nFactorize sum and try all poosible factors..\nfor each factor i the target is sum/i;\n\nnow to check if each factor can be our answer we are runni
\n# Approach\nFactorize sum and try all poosible factors..\nfor each factor i the target is sum/i;\n\nnow to check if each factor can be our answer we are running a dfs, \nin which if any component reaches the sum of exactly targeted value (val is used in code), then we return 0 for that component or we can say we deleted that.. \nat at the end if we are able to delete the root also then we are good and this factor is one of our answer..\n\n# Complexity\n- Time complexity: O( sqrt(sum) * N )\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>>g;\n    vector<int>A;\n\n    // dfs function will check whether split is possible or not\n    // if it is possible then it will return 0;\n    int dfs(int r,int prnt,int val){\n        int c=A[r];\n        for(auto i:g[r]){\n            if(i!=prnt){\n                c+=dfs(i,r,val);\n            }\n        }\n        if(c==val)return 0;\n        else return c;\n    }\n    int componentValue(vector<int>& a, vector<vector<int>>& ed) {\n        int s=0;\n        A=a;\n        for(auto i:a)s+=i;\n\n        // factorizing sum\n        vector<int>f;\n        for(int i=1;i*i<=s;i++){\n            if(s%i==0){\n                f.push_back(i);\n                if(s/i!=i)f.push_back(s/i);\n            }\n        }\n\n        // making tree\n        int n=a.size(),ans=1;\n        g.assign(n+1,vector<int>());\n        for(auto i:ed){\n            g[i[0]].push_back(i[1]);\n            g[i[1]].push_back(i[0]);\n        }\n\n        // checking for each factor, can it be our ans?\n        for(auto i:f){\n            if(i<=n&&!dfs(0,-1,s/i)){\n                ans=max(ans,i);\n            }\n        }\n        return ans-1;\n    }\n};\n```
['Math', 'Tree', 'Depth-First Search', 'Enumeration', 'C++']