python3 DFS
possible component sums should be a factor of the total sum and larger than the biggest element in M, as well as a possible subtree sum.\n\ntry every component sum satisfying these conditions.\n# Code\n```python3 []\nclass Solution:\n    def componentValue(self, nums: List[int], edges: List[List[int]]) -> int:\n        N = len(nums)\n        m = min(nums)\n        M = max(nums)\n        graph = {i:set([]) for i in range(N)}\n        for u,v in edges:\n            graph[u].add(v)\n            graph[v].add(u)\n        S = sum(nums)\n        factors = set([])\n        for i in range(1,int(sqrt(S))+2):\n            if S % i == 0:\n                if i>=M:factors.add(i)\n                if S // i >= M: factors.add(S//i)\n        self.subtreesum = [0]*N\n        @cache\n        def dfs(node,parent):\n            res = nums[node]\n            for child in graph[node]:\n                if child == parent:\n                    continue\n                res += dfs(child,node)\n            self.subtreesum[node] = res\n            return res\n        dfs(0,-1)\n        Sums = set(self.subtreesum)\n        factors = factors&Sums\n        self.reses = {f:0 for f in factors}\n        @cache\n        def dfs2(node,parent,f):\n            currs = nums[node]\n            for child in graph[node]:\n                if child == parent:continue\n                currs += dfs2(child,node,f)\n            if currs == f:\n                currs = 0\n                self.reses[f]+=1\n            if node == 0 and currs!=0: self.reses[f] = 0\n            return currs\n        for f in factors:\n            dfs2(0,-1,f)\n        return max(self.reses[a] for a in self.reses)-1\n\n\n\n\n\n\n\n\n        \n```
['Depth-First Search', 'Python3']