Only Do N Times DFS | [CPP] | Explanation
\nclass Solution {\npublic:\n    vector<int> arr;\n    vector<vector<int>> g;\n    \n    int dfs(int idx, int parent, const int target){\n        int val = arr[
```\nclass Solution {\npublic:\n    vector<int> arr;\n    vector<vector<int>> g;\n    \n    int dfs(int idx, int parent, const int target){\n        int val = arr[idx];\n        for(auto nbr : g[idx]){\n            if(nbr == parent) continue;\n            val += dfs(nbr,idx,target);\n        }\n        \n        if(val == target) return 0;   // we find any one componets give me target value then return 0 bcz we consider that one components\n        return val;\n    }\n    \n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int n = nums.size(), ttl_sum = 0, res = 0;\n        arr.resize(n); g.resize(n);\n        \n        // Create graph using given edges\n        for(auto i : edges){\n            g[i[0]].push_back(i[1]);\n            g[i[1]].push_back(i[0]);\n        }\n        \n        // Finding total sum of all nodes value \n        for(int i = 0; i<n; i++){\n            ttl_sum += nums[i];\n            arr[i] = nums[i];\n        }\n        // We find one by one components with target sum % p ---> 1 <= p <= no of nodes\n        // bcz we find min one components and max no of nodes components of same value\n        // that\'s why we do n time dfs to find we get sum % p vlaue components or not\n        for(int p = 1; p <= n; p++){\n            if(ttl_sum % p != 0) continue;\n            // we get return 0 then we said we create p components of same values and deleted p-1 edges in a tree or graph\n            if(dfs(0,-1,ttl_sum/p) != 0) continue;\n            res = max(res,p-1);\n        }\n        \n        return res;\n    }\n};\n```
['Depth-First Search', 'Graph', 'C', 'C++']