DFS
Code
# Code
```cpp []
class Solution {
public:
    void dfs(int node, int parent, vector<vector<int>>& adj,
             vector<int>& values, vector<int>& a, int& comp, int& target) {
        values[node] = a[node];
        for (auto adjNode : adj[node]) {
            if (adjNode == parent)
                continue;
            dfs(adjNode, node, adj, values, a, comp, target);
            values[node] += values[adjNode];
        }
        if (values[node] == target) {
            comp++;
            values[node] = 0;
        }
    }
    int componentValue(vector<int>& a, vector<vector<int>>& edges) {
        int n = a.size();
        vector<vector<int>> adj(n);
        for (auto i : edges) {
            adj[i[0]].push_back(i[1]);
            adj[i[1]].push_back(i[0]);
        }
        int sum = accumulate(a.begin(), a.end(), 0), ans = 0;
        if (sum == n)
            return n - 1;
        for (int i = 2; i * i <= sum; i++) {
            if (sum % i)
                continue;
            int j = sum / i;
            vector<int> values(n, 0);
            int comp = 0;
            dfs(0, -1, adj, values, a, comp, j);
            if ((comp * j) == sum)
                ans = max(ans, comp - 1);
            j = i, comp = 0;
            values.resize(n, 0);
            dfs(0, -1, adj, values, a, comp, j);
            if ((comp * j) == sum)
                ans = max(ans, comp - 1);
        }
        return ans;
    }
};
```
['C++']