Java
for each possible sum (that can be devided between nodes to fit total)Start from the leaves and bfs, at any time add to the Queue just nodes that have up to 1 u
for each possible sum (that can be devided between nodes to fit total)Start from the leaves and bfs, at any time add to the Queue just nodes that have up to 1 unseen neighbor, code explains the details.\n\n\n# Code\n```\nclass Solution {\n    public int componentValue(int[] nums, int[][] edges) {\n        int n = nums.length, total = 0, root = -1; \n        for (int num : nums) total += num;\n        List [] egdesFrom = new List[n];\n        for (int i = 0; i < n; i++) egdesFrom[i] = new ArrayList();\n        for (int [] e : edges) {\n            egdesFrom[e[0]].add(e[1]);\n            egdesFrom[e[1]].add(e[0]);\n        }\n        List<Integer> leaves = new ArrayList();\n        for (int i = 0; i < n; i++) if (egdesFrom[i].size() == 1) leaves.add(i); // nodes with one edge in the graph -> only one diraction to go to add to sum\n\n        for (int ans = n - 1; ans > 0; ans--){ // try to delete ans edges\n            if (total % (ans + 1) != 0) continue; // leads to ans + 1 components\n            int sum = total / (ans + 1); // sum of each component\n            if (leavesUpSum(sum, egdesFrom, Arrays.copyOf(nums, n), leaves)) return ans;\n        }\n        return 0;\n    }\n\n    private boolean leavesUpSum(int sum, List [] graph, int[] nums, List<Integer> leaves){\n        boolean seen[] = new boolean[nums.length];\n        Queue<Integer> q = new LinkedList();\n        for (int leaf : leaves) q.add(leaf);\n        while(!q.isEmpty()){ // bfs\n            int size = q.size();\n            for (int j = 0; j < size; j++){\n                int cur = q.poll(), i = 0, neighbor = -1;\n                if (seen[cur]) continue;\n                if (nums[cur] > sum) return false;\n                // find the unseen neighbor if exist (can\'t have two unseen neighbors - otherwise not in Q yet)\n                while (i < graph[cur].size() && seen[(int)graph[cur].get(i)]) i++; \n                if (i < graph[cur].size()) neighbor = (int)graph[cur].get(i);\n\n                seen[cur] = true;\n                if (nums[cur] < sum) {\n                    if (neighbor == -1) return false;\n                    nums[neighbor] += nums[cur];\n                }\n                if (neighbor != -1) q.offer(neighbor);\n            }\n        }\n        return true;\n    }\n}\n```
['Java']