Normal DFS approach, but with prime factorization to minimize search space, Clean code
Intuition\nThe main two insights here is that you need to root the tree at a given node, and that whatever sum each group of nodes have, it needs to be able to 
# Intuition\nThe main two insights here is that you need to root the tree at a given node, and that whatever sum each group of nodes have, it needs to be able to divide the sum of all nodes.\n\n# Approach\n\nMy approach is fairly standard, the only difference is that I figure out the prime factors of the sum, and using them, I only search for valid divisors of the given number. In practice this is not much faster, but it does cut down the search space by a lot.\n\n# Complexity\n- Time complexity: I don\'t know\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: I don\'t know\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\ninline const auto optimize = []() {\n    std::ios::sync_with_stdio(false);\n    std::cin.tie(nullptr);\n    std::cout.tie(nullptr);\n    return 0;\n}();\n\nclass Solution {\n\n    using GRAPH = std::vector<std::vector<int>>;\n\n    static constexpr int dfs(const GRAPH& G, const std::vector<int>& nums,\n                             const int target, const int parent,\n                             const int cur) {\n        std::int64_t sum = nums[cur];\n\n        for (const auto next : G[cur]) {\n            if (next == parent)\n                continue;\n\n            sum += dfs(G, nums, target, cur, next);\n            if (target < sum)\n                return std::numeric_limits<int>::max();\n        }\n\n        return (sum == target) ? 0 : sum;\n    }\n\n    static constexpr int tryAllPrimes(const int numSum, const GRAPH& G,\n                            const std::vector<int>& nums,\n                            const std::vector<std::pair<int, int>>& primes,\n                            const int soFar, const int curIdx) {\n        if (curIdx == -1) {\n            const int target = numSum / soFar;\n            if (dfs(G, nums, target, -1, 0) == 0)\n                return soFar - 1;\n            else\n                return 0;\n        }\n\n        const auto [prime, cnt] = primes[curIdx];\n        int cur = soFar;\n        for (int i = 0; i <= cnt; i++) {\n            const int res =\n                tryAllPrimes(numSum, G, nums, primes, cur, curIdx - 1);\n            if (0 < res) {\n                return res;\n            }\n\n            cur /= prime;\n        }\n\n        return 0;\n    }\n\npublic:\n    static constexpr int\n    componentValue(const std::vector<int>& nums,\n                   const std::vector<std::vector<int>>& edges) {\n        const int N = nums.size();\n        const int numSum = std::reduce(nums.begin(), nums.end(), 0);\n\n        GRAPH G(N);\n        for (const auto& edge : edges) {\n            const auto a = edge[0];\n            const auto b = edge[1];\n            G[a].push_back(b);\n            G[b].push_back(a);\n        }\n\n        auto primes = primeFactor(N, numSum);\n\n        return tryAllPrimes(numSum, G, nums, primes, numSum, primes.size()-1);\n    }\n\nprivate:\n    static constexpr std::vector<std::pair<int, int>>\n    primeFactor(const int N, const int target) {\n        // Let\'s get all the primes up to N, which is the maximum number of\n        // parts we can split it into\n        const int maxPrime = static_cast<int>(std::sqrt(N)) + 1;\n\n        // The max prime we want to test is N\n        // because primes above that, is not interesting to us.\n\n        std::vector<unsigned char> isComp(maxPrime + 1);\n        std::vector<std::pair<int, int>> primes;\n\n        int curTarget = target;\n        for (int i = 2; i <= maxPrime; i++) {\n            if (isComp[i])\n                continue;\n\n            if (curTarget < i) {\n                break;\n            }\n\n            int times = 0;\n            while (i <= curTarget && (curTarget % i == 0)) {\n                times++;\n                curTarget /= i;\n            }\n            if (0 < times) {\n                primes.emplace_back(i, times);\n            }\n\n            for (int j = i + i; j <= maxPrime; j += i) {\n                isComp[j] = true;\n            }\n        }\n\n        if (1 < curTarget) {\n            primes.emplace_back(curTarget, 1);\n        }\n\n        return primes;\n    }\n};\n```
['Math', 'Tree', 'Depth-First Search', 'Graph', 'Number Theory', 'C++']