Test2
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

class Solution {
public:
    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {
        int n = nums.size();
        int total_sum = accumulate(nums.begin(), nums.end(), 0);
        vector<int> divisors = getDivisors(total_sum);
        
        vector<vector<int>> adj(n);
        for (auto& e : edges) {
            adj[e[0]].push_back(e[1]);
            adj[e[1]].push_back(e[0]);
        }
        
        for (int k : divisors) {
            int d = total_sum / k;
            if (d == 0) continue;
            if (isPossible(d, adj, nums, total_sum)) {
                return k - 1;
            }
        }
        
        return 0;
    }

private:
    vector<int> getDivisors(int S) {
        vector<int> divisors;
        for (int i = 1; i * i <= S; ++i) {
            if (S % i == 0) {
                divisors.push_back(i);
                if (i != S / i) {
                    divisors.push_back(S / i);
                }
            }
        }
        sort(divisors.rbegin(), divisors.rend());
        return divisors;
    }

    bool isPossible(int d, vector<vector<int>>& adj, vector<int>& nums, int total_sum) {
        int count = 0;
        int sum = dfs(0, -1, d, count, adj, nums);
        return (sum == 0 && count == (total_sum / d));
    }

    int dfs(int node, int parent, int d, int& count, vector<vector<int>>& adj, vector<int>& nums) {
        int sum = nums[node];
        for (int child : adj[node]) {
            if (child == parent) continue;
            int child_sum = dfs(child, node, d, count, adj, nums);
            if (child_sum == -1) {
                return -1;
            }
            sum += child_sum;
            if (sum > d) {
                return -1;
            }
        }
        if (sum == d) {
            ++count;
            return 0;
        }
        return sum;
    }
};
```
['C++']