-
Notifications
You must be signed in to change notification settings - Fork 0
/
zad1.py
42 lines (33 loc) · 1007 Bytes
/
zad1.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
class Node:
def __init__(self):
self.children = 0
self.child = []
def heavy_path(T):
global_max = 0
def recur(node):
# Loop over all node's children and find two (if there are)
# longest paths
max_paths = [0, 0]
for child, weight in node.child:
length = recur(child) + weight
update_paths(max_paths, length)
# Get the max-length path length which ends in the current node
curr_max = max(max_paths)
# Update globally longest path
nonlocal global_max
global_max = max(global_max, curr_max, sum(max_paths))
return curr_max
recur(T)
return global_max
def update_paths(paths, length):
if paths[0] < paths[1] and length > paths[0]:
paths[0] = length
elif length > paths[1]:
paths[1] = length
if __name__ == '__main__':
A = Node()
B = Node()
C = Node()
A.children = 2
A.child = [(B, 5), (C, -1)]
print(heavy_path(A))