-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcalcium2015.py
57 lines (48 loc) · 1.7 KB
/
calcium2015.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
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left