-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path华为30_3.py
36 lines (31 loc) · 887 Bytes
/
华为30_3.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
from collections import defaultdict
n = int(input())
needs = list(map(int,input().split()))
graph = defaultdict(list)
in_degree = [0]*n
for i in range(n):
tmp = list(map(int,input().split()))
for j in range(n):
if tmp[j] ==1:
graph[j].append(i)
in_degree[i]+=1
def topo(n, graph, need,in_degree):
zero_degree = set()
for i in range(n):
if in_degree[i] == 0:
zero_degree.add(i)
max_memory = 0
while zero_degree:
cur_need = 0
nex = set()
while zero_degree:
q = zero_degree.pop()
cur_need+=need[q]
for j in graph[q]:
in_degree[j]-=1
if in_degree[j] == 0:
nex.add(j)
max_memory = max(max_memory,cur_need)
zero_degree = nex
return max_memory
print(topo(n,graph,needs,in_degree))