[Python] DFS+Factors Easy
Reply if you need any more clarification\n\nclass Solution:\n    def componentValue(self, nums: List[int], edges: List[List[int]]) -> int:\n        # Get All Fa
Reply if you need any more clarification\n```\nclass Solution:\n    def componentValue(self, nums: List[int], edges: List[List[int]]) -> int:\n        # Get All Factors\n        factors = set()\n        totalTreeSum = sum(nums)\n        for factor in range(1,ceil(sqrt(totalTreeSum))):\n            if totalTreeSum%factor == 0:\n                factors.add(factor)\n                factors.add(totalTreeSum//factor)\n        maxNum = max(nums)\n        \n        graph = defaultdict(list)\n        \n        for u, v in edges:\n            graph[u].append(v)\n            graph[v].append(u)\n        \n        def dfs(u,Sum, vis):\n            vis.add(u)\n            NowSum = nums[u]\n            for v in graph[u]:\n                if v not in vis:\n                    NowSum += dfs(v, Sum, vis)\n            if NowSum == Sum:\n                self.components-=1\n                return 0\n            elif NowSum < Sum:\n                return NowSum\n            return 0\n            \n            \n        def isPossible(targetSum, components):\n            self.components = components\n            dfs(0,targetSum,set())\n            return self.components == 0\n            \n        for factor in sorted(list(factors)):\n            # Each component should be atleast maxNum to be made\n            if factor < maxNum: continue\n            numberOfComponentsPossible = totalTreeSum//factor\n            if isPossible(factor,numberOfComponentsPossible):\n                return numberOfComponentsPossible-1\n        return 0\n            \n        \n```
['Math', 'Depth-First Search', 'Graph', 'Python']