-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchain_reactions_2.py
56 lines (47 loc) · 1.45 KB
/
chain_reactions_2.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
import copy
def build_tree(fun, point):
tree = []
for val, linked in zip(fun, point):
tree.append([val, linked])
return tree
def run_tree(tree, parent_nodes):
result = 0
for node in parent_nodes:
val = []
for t in tree:
if t[1] == node:
val.append(t[0])
# print('val: {}'.format(val))
index_min = min(range(len(val)), key=val.__getitem__)
# print(index_min)
for idx, values in enumerate(val):
if idx == index_min:
# compare value with parent node
if values > tree[node-1][0]:
tree[node-1][0] = values
continue
else:
result += values
for last in tree:
if last[1] == 0:
result += last[0]
# print('results: {}'.format(result))
return result
def solve(n, fun, point):
# build tree
tree = build_tree(fun, point)
# print(tree)
parent_nodes = list(set(point))
parent_nodes.remove(0)
parent_nodes.sort(reverse=True)
# print(parent_nodes)
temp_tree = copy.deepcopy(tree)
return run_tree(temp_tree, parent_nodes)
if __name__ == '__main__':
t = int(input())
for case in range(1, t+1):
n = int(input())
fun = list(map(int, input().split()))
point = list(map(int, input().split()))
result = solve(n, fun, point)
print('Case #{}: {}'.format(case, result))