-
Notifications
You must be signed in to change notification settings - Fork 0
/
1005_topological_sort.py
56 lines (46 loc) ยท 1.26 KB
/
1005_topological_sort.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 sys
from collections import deque
input = sys.stdin.readline
T = int(input())
def topological_sort(graph, degree):
s = deque()
length = len(degree) - 1
ans = []
for i in range(1, length + 1):
if degree[i] == 0:
s.append(i)
degree[i] = -1
for i in range(length):
element = s.pop()
ans.append(element)
for j in range(len(graph[element])):
degree[graph[element][j]] -= 1
for j in range(1, length + 1):
if degree[j] == 0:
s.append(j)
degree[j] = -1
return ans
for _ in range(T):
n, k = map(int, input().rstrip().split())
arr = [0] + list(map(int, input().rstrip().split()))
graph = [[] for _ in range(n + 1)]
graph_reverse = [[] for _ in range(n + 1)]
degree = [0 for _ in range(n + 1)]
for _ in range(k):
x, y = map(int, input().rstrip().split())
graph[x].append(y)
graph_reverse[y].append(x)
degree[y] += 1
w = int(input())
tp = topological_sort(graph, degree)
dp = [0 for _ in range(n + 1)]
for i in range(0, n):
max_ = 0
this = tp[i]
if len(graph_reverse[this]) == 0:
dp[this] = arr[this]
else:
for j in range(len(graph_reverse[this])):
max_ = max(max_, dp[graph_reverse[this][j]])
dp[this] = max_ + arr[this]
print(dp[w])