Implementation
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires splitting a tree into connected components such that each componen
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires splitting a tree into connected components such that each component has the same sum of node values. Since the structure is a tree, we know there are \uD835\uDC5B \u2212 1 edges, and removing edges creates separate components.\n\nThe key insight is that the target sum for each component must be a divisor of the total sum of node values (\uD835\uDC46). By iterating over the divisors of \uD835\uDC46\n\nS, we can test if it\'s possible to divide the tree into components with the target sum.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCalculate the Total Sum:\n\nCompute \uD835\uDC46 the total sum of all node values.\nDetermine all divisors of \uD835\uDC46, as only these divisors can be possible values for the target sum of each component.\n\n- Check for Valid Divisors:\n\nFor each divisor k, calculate targetSum = \uD835\uDC46/\uD835\uDC58\n\nVerify if it\'s feasible to split the tree into \uD835\uDC58 components with a sum of targetSum.\n\n- DFS for Validation:\n\nPerform a depth-first search (DFS) to calculate the sum of nodes in each subtree.\n\nIf a subtree\'s sum equals targetSum, it is considered a valid component, and the DFS continues for the rest of the tree.\n\nIf a subtree\u2019s sum exceeds targetSum, the divisor \uD835\uDC58 is invalid, and we try the next divisor.\n\n- Result:\n\nThe maximum \uD835\uDC58\u22121 for which the tree can be divided is the maximum number of edges that can be removed.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```csharp []\npublic class Solution \n{\n    private int[] nums;\n\n    public int ComponentValue(int[] nums, int[][] edges) \n    {\n        int n = nums.Length;\n        this.nums = nums;\n\n        var graph = new List<int>[n];\n        for (int i = 0; i < n; i++) {\n            graph[i] = new List<int>();\n        }\n        foreach (var edge in edges) {\n            graph[edge[0]].Add(edge[1]);\n            graph[edge[1]].Add(edge[0]);\n        }\n\n        int sum = nums.Sum();\n\n        for (int k = n; k > 0; k--) {\n            if (sum % k != 0) continue;\n            int target = sum / k;\n\n            if (Helper(graph, 0, -1, target) == 0) {\n                return k - 1; \n            }\n        }\n\n        return 0;\n    }\n\n    private int Helper(List<int>[] graph, int node, int parent, int target) {\n        // Caso base: nodo hoja\n        if (graph[node].Count == 1 && graph[node][0] == parent) {\n            if (nums[node] > target) return -1;\n            return nums[node] == target ? 0 : nums[node];\n        }\n\n        int sum = nums[node];\n        foreach (int neighbor in graph[node]) {\n            if (neighbor == parent) continue;\n            int result = Helper(graph, neighbor, node, target);\n            if (result == -1) return -1;\n            sum += result;\n        }\n\n        // Validar la suma obtenida\n        if (sum > target) return -1;\n        return sum == target ? 0 : sum;\n    }\n}\n```
['C#']