Java Solution Topological Sort
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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```\nclass Solution {\n    public int componentValue(int[] nums, int[][] edges) {\n        int n = nums.length;\n        if(n == 1){\n            return 0;\n        }\n        Map<Integer, List<Integer>> map = new HashMap<>();\n        int[] indegree = new int[n];\n        for(int[] edge : edges){\n            int current = edge[0];\n            int next = edge[1];\n            map.putIfAbsent(current, new ArrayList<>());\n            map.get(current).add(next);\n            indegree[current]++;\n            map.putIfAbsent(next, new ArrayList<>());\n            map.get(next).add(current);\n            indegree[next]++;\n        }\n        int total = 0;\n        for(int num : nums){\n            total += num;\n        }\n        List<Integer> factors = new ArrayList<>();\n        for(int i = 1; i * i <= total; i++){\n            if(total % i != 0){\n                continue;\n            }\n            factors.add(i);\n            factors.add(total / i);\n        }\n        Collections.sort(factors);\n        for(int factor : factors){\n            Set<Integer> visited = new HashSet<>();\n            Queue<Integer> queue = new ArrayDeque<>();\n            int[] indegreenew = new int[n];\n            int[] sum = new int[n];\n            for(int i = 0; i < n; i++){\n                indegreenew[i] = indegree[i];\n                sum[i] = nums[i];\n            }\n            for(int i = 0; i < n; i++){\n                if(indegreenew[i] == 1){\n                    queue.offer(i);\n                    visited.add(i);\n                }\n            }\n            boolean flag = false;\n            while(!queue.isEmpty()){\n                int current = queue.poll();\n                if(sum[current] > factor){\n                    flag = true;\n                    break;\n                }\n                else if(sum[current] == factor){\n                    sum[current] = 0;\n                }\n                List<Integer> neighbors = map.get(current);\n                for(int nei : neighbors){\n                    if(visited.contains(nei)){\n                        continue;\n                    }\n                    indegreenew[nei]--;\n                    sum[nei] += sum[current];\n                    if(indegreenew[nei] == 1){\n                        queue.offer(nei);\n                        visited.add(nei);\n                    }\n                }\n            }\n            if(!flag){\n                return total/factor - 1;\n            }\n        }\n        return 0;\n    }\n}\n```
['Java']