-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximize-sum-of-weights-after-edge-removals.py
129 lines (116 loc) · 4.4 KB
/
maximize-sum-of-weights-after-edge-removals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Time: O(n)
# Space: O(n)
import random
# iterative dfs, quick select
class Solution(object):
def maximizeSumOfWeights(self, edges, k):
"""
:type edges: List[List[int]]
:type k: int
:rtype: int
"""
def nth_element(nums, n, compare=lambda a, b: a < b):
def tri_partition(nums, left, right, target):
i = left
while i <= right:
if compare(nums[i], target):
nums[i], nums[left] = nums[left], nums[i]
left += 1
i += 1
elif compare(target, nums[i]):
nums[i], nums[right] = nums[right], nums[i]
right -= 1
else:
i += 1
return left, right
left, right = 0, len(nums)-1
while left <= right:
pivot_idx = random.randint(left, right)
pivot_left, pivot_right = tri_partition(nums, left, right, nums[pivot_idx])
if pivot_left <= n <= pivot_right:
return
elif pivot_left > n:
right = pivot_left-1
else: # pivot_right < n.
left = pivot_right+1
def iter_dfs():
cnt = [[0]*2 for _ in xrange(len(adj))]
stk = [(1, 0, -1)]
while stk:
step, u, p = stk.pop()
if step == 1:
stk.append((2, u, p))
for v, w in reversed(adj[u]):
if v == p:
continue
stk.append((1, v, u))
elif step == 2:
curr = 0
diff = []
for v, w in adj[u]:
if v == p:
continue
curr += cnt[v][0]
diff.append(max((cnt[v][1]+w)-cnt[v][0], 0))
if k-1 < len(diff):
nth_element(diff, k-1, lambda a, b: a > b)
cnt[u][0] = curr+sum(diff[i] for i in xrange(min(k, len(diff))))
cnt[u][1] = curr+sum(diff[i] for i in xrange(min(k-1, len(diff))))
return cnt[0][0]
adj = [[] for _ in xrange(len(edges)+1)]
for u, v, w in edges:
adj[u].append((v, w))
adj[v].append((u, w))
return iter_dfs()
# Time: O(n)
# Space: O(n)
import random
# dfs, quick select
class Solution2(object):
def maximizeSumOfWeights(self, edges, k):
"""
:type edges: List[List[int]]
:type k: int
:rtype: int
"""
def nth_element(nums, n, compare=lambda a, b: a < b):
def tri_partition(nums, left, right, target):
i = left
while i <= right:
if compare(nums[i], target):
nums[i], nums[left] = nums[left], nums[i]
left += 1
i += 1
elif compare(target, nums[i]):
nums[i], nums[right] = nums[right], nums[i]
right -= 1
else:
i += 1
return left, right
left, right = 0, len(nums)-1
while left <= right:
pivot_idx = random.randint(left, right)
pivot_left, pivot_right = tri_partition(nums, left, right, nums[pivot_idx])
if pivot_left <= n <= pivot_right:
return
elif pivot_left > n:
right = pivot_left-1
else: # pivot_right < n.
left = pivot_right+1
def dfs(u, p):
result = 0
diff = []
for v, w in adj[u]:
if v == p:
continue
cnt = dfs(v, u)
result += cnt[0]
diff.append(max((cnt[1]+w)-cnt[0], 0))
if k-1 < len(diff):
nth_element(diff, k-1, lambda a, b: a > b)
return (result+sum(diff[i] for i in xrange(min(k, len(diff)))), result+sum(diff[i] for i in xrange(min(k-1, len(diff)))))
adj = [[] for _ in xrange(len(edges)+1)]
for u, v, w in edges:
adj[u].append((v, w))
adj[v].append((u, w))
return dfs(0, -1)[0]