|
| 1 | +# Time: O(n) |
| 2 | +# Space: O(n) |
| 3 | + |
| 4 | +# bfs, greedy |
| 5 | +class Solution(object): |
| 6 | + def componentValue(self, nums, edges): |
| 7 | + """ |
| 8 | + :type nums: List[int] |
| 9 | + :type edges: List[List[int]] |
| 10 | + :rtype: int |
| 11 | + """ |
| 12 | + def bfs(target): |
| 13 | + total = nums[:] |
| 14 | + lookup = [len(adj[u]) for u in xrange(len(adj))] |
| 15 | + q = [u for u in xrange(len(adj)) if lookup[u] == 1] |
| 16 | + while q: |
| 17 | + new_q = [] |
| 18 | + for u in q: |
| 19 | + if total[u] > target: |
| 20 | + return False |
| 21 | + if total[u] == target: |
| 22 | + total[u] = 0 |
| 23 | + for v in adj[u]: |
| 24 | + total[v] += total[u] |
| 25 | + lookup[v] -= 1 |
| 26 | + if lookup[v] == 1: |
| 27 | + new_q.append(v) |
| 28 | + q = new_q |
| 29 | + return True |
| 30 | + |
| 31 | + result = 0 |
| 32 | + adj = [[] for _ in xrange(len(nums))] |
| 33 | + for u, v in edges: |
| 34 | + adj[u].append(v) |
| 35 | + adj[v].append(u) |
| 36 | + total = sum(nums) |
| 37 | + for cnt in reversed(xrange(2, len(nums)+1)): |
| 38 | + if total%cnt == 0 and bfs(total//cnt): |
| 39 | + return cnt-1 |
| 40 | + return 0 |
| 41 | + |
| 42 | + |
| 43 | +# Time: O(n) |
| 44 | +# Space: O(n) |
| 45 | +# iterative dfs, greedy |
| 46 | +class Solution2(object): |
| 47 | + def componentValue(self, nums, edges): |
| 48 | + """ |
| 49 | + :type nums: List[int] |
| 50 | + :type edges: List[List[int]] |
| 51 | + :rtype: int |
| 52 | + """ |
| 53 | + def iter_dfs(target): |
| 54 | + total = nums[:] |
| 55 | + stk = [(1, (0, -1))] |
| 56 | + while stk: |
| 57 | + step, (u, p) = stk.pop() |
| 58 | + if step == 1: |
| 59 | + stk.append((2, (u, p))) |
| 60 | + for v in adj[u]: |
| 61 | + if v == p: |
| 62 | + continue |
| 63 | + stk.append((1, (v, u))) |
| 64 | + elif step == 2: |
| 65 | + for v in adj[u]: |
| 66 | + if v == p: |
| 67 | + continue |
| 68 | + total[u] += total[v] |
| 69 | + if total[u] == target: |
| 70 | + total[u] = 0 |
| 71 | + return total[0] |
| 72 | + |
| 73 | + result = 0 |
| 74 | + adj = [[] for _ in xrange(len(nums))] |
| 75 | + for u, v in edges: |
| 76 | + adj[u].append(v) |
| 77 | + adj[v].append(u) |
| 78 | + total = sum(nums) |
| 79 | + for cnt in reversed(xrange(2, len(nums)+1)): |
| 80 | + if total%cnt == 0 and iter_dfs(total//cnt) == 0: |
| 81 | + return cnt-1 |
| 82 | + return 0 |
| 83 | + |
| 84 | + |
| 85 | +# Time: O(n) |
| 86 | +# Space: O(n) |
| 87 | +# dfs, greedy |
| 88 | +class Solution3(object): |
| 89 | + def componentValue(self, nums, edges): |
| 90 | + """ |
| 91 | + :type nums: List[int] |
| 92 | + :type edges: List[List[int]] |
| 93 | + :rtype: int |
| 94 | + """ |
| 95 | + def dfs(u, p, target): |
| 96 | + total = nums[u] |
| 97 | + for v in adj[u]: |
| 98 | + if v == p: |
| 99 | + continue |
| 100 | + total += dfs(v, u, target) |
| 101 | + return total if total != target else 0 |
| 102 | + |
| 103 | + result = 0 |
| 104 | + adj = [[] for _ in xrange(len(nums))] |
| 105 | + for u, v in edges: |
| 106 | + adj[u].append(v) |
| 107 | + adj[v].append(u) |
| 108 | + total = sum(nums) |
| 109 | + for cnt in reversed(xrange(2, len(nums)+1)): |
| 110 | + if total%cnt == 0 and dfs(0, -1, total//cnt) == 0: |
| 111 | + return cnt-1 |
| 112 | + return 0 |
0 commit comments