Solution based in intuiton (full explained)
Intuition\nWe define W as the total weight of the tree and x as the total weight of each component after erase some edges and finally k as the number of compone
# Intuition\nWe define **W** as the total weight of the tree and **x** as the total weight of each component after erase some edges and finally **k** as the number of components left\n\nFirst of all we know that if a tree can be divided into many components with the same weight **x** the total weight of the tree must be a multiple of x that is **W = kx** for some **k** then we can iterate over all divisors of the total weight of the tree and try to find if it can be partitioned into k components such that they all have weight = **x**\n\nAnd here\'s the main intuition, if we need to make each component weigh = **x**, we know we\'ll get $$\\frac{W}{x} = k$$ components, so there must be exactly k subtrees with weight equal to a multiple of **x**\n\n# Example \nWe use the same tree of first example \n![image.png](https://assets.leetcode.com/users/images/205100cf-f59e-47d1-9aac-4db1c2b66400_1665851155.6040244.png)\n\nFirst we get the weight of each subtree (the root doesn\'t matter)\n\nFor our example, the weights of each subtree with root 0 from node 0 to n-1 are:\n**[18,12,2,8,6]**\n\nAnd then we try to find the solution for some x. \n\nFor x = 9: (remember that **x** is some divisor of **W**)\nwe find that only one of the subtrees has weight = **kx** but intuition says we need to find exactly $$\\frac{W}{x} = \\frac{18}{9} =2 $$ so this divisor is invalid\n\nFor x = 6:\nIn this case we find that the subtree of nodes **[0, 1, 4]** with weights **[18, 12, 6]** are multiples of **x = 6** and we have exactly 3 which is equal to **$$k = \\frac{W}{x} = \\frac{18}{6} = 3 $$** so we can say that it is a valid division of the tree, that is, there must be some way to delete some edges (exactly k-1) so that each component has weight = **x**\n\n\n# Approach sumary\nIterate over all divisors **x** of **W** and find if there are exactly **$$\\frac{W}{x}$$** subtrees whose weight $$w_{i} \\% x = 0 $$ and if this condition is valid we update the answer with **$$\\max( ans, \\frac{W}{x} -1)$$** (ans at the beginning is 0) \n# Complexity\n$$O(sqrt(W) \\times n)$$ (\\sqrt doesn\'t render)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<bool> vis;\n    vector<int> Nums;\n    vector<int> graph[20007];\n    vector<int> sz;\n    void solve(int u){\n        sz[u] = Nums[u];\n        vis[u] = true;\n        for(auto v:graph[u]){\n            if(vis[v])continue;\n            solve(v);\n            sz[u]+=sz[v];\n        }\n    }\n    \n    int componentValue(vector<int>& nums, vector<vector<int>>& edges) {\n        int n = nums.size();\n        Nums = nums;\n        vis.resize(n);\n        sz.resize(n);\n        // Create the graph with adjacent list\n        for(int i = 0;i<edges.size();i++){\n            int u = edges[i][0];\n            int v = edges[i][1];\n            graph[u].push_back(v);\n            graph[v].push_back(u);\n        }\n        int ans = 0;\n        \n        // Dfs to get the weight of each subtree (well known algorithm)\n        // The root does not matter\n        solve(0);\n\n        // Define W as the total weight of the tree\n        int W = accumulate(nums.begin(),nums.end(),0);\n        \n        for(int x = 1;x*x<=W;x++){\n            if(W%x ==0){\n                // Check for every divisor\n                int cont = 0;\n                for(int j = 0;j<n;j++){\n                    // Count every subtree with weight multiple of x\n                    if(sz[j]%x ==0){\n                        cont++;\n                    }\n                }\n                int k = W/x;\n                // Check the condition that must be exactly W/x components and update the answer\n                // To make k components in a tree we need to erase k-1 edges \n                if(cont == k )\n                    ans = max(ans,k-1);\n                \n                if((W/x)==x)continue;\n\n                // The same for the other divisor W/x\n                int otherDiv = W/x;\n                cont = 0;\n                for(int j = 0;j<n;j++){\n                    if(sz[j]%otherDiv==0){\n                        cont++;\n                    }\n                }\n                k = W/otherDiv;\n                if(cont==k)\n                    ans = max(ans,k-1);\n            }\n        }\n        return ans;\n        \n    }\n};\n```
['C++']