-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1005.cpp
More file actions
52 lines (52 loc) · 1 KB
/
1005.cpp
File metadata and controls
52 lines (52 loc) · 1 KB
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
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int num[1001], dp[1001], in[1001];
bool visit[1001];
vector<int> graph[1001];
stack<int> sorted;
void dfs(int now) {
visit[now] = true;
for (int i = 0; i < graph[now].size(); i++) {
int to = graph[now][i];
if (visit[to])continue;
dfs(to);
}
sorted.push(now);
}
void dp_dfs(int now) {
for (int i = 0; i < graph[now].size(); i++) {
int to = graph[now][i];
dp[to] = max(dp[to], dp[now] + num[to]);
}
}
int main() {
int T, N, K, a, b, W;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &N, &K);
for (int i = 1; i <= N; i++) {
scanf("%d", &num[i]);
dp[i] = num[i];
graph[i].clear();
in[i] = 0;
visit[i] = false;
}
while (K--) {
scanf("%d %d", &a, &b);
graph[a].push_back(b);
in[b]++;
}
for (int i = 1; i <= N; i++)if(in[i]==0)dfs(i);
while (!sorted.empty()) {
dp_dfs(sorted.top());
sorted.pop();
}
scanf("%d", &W);
printf("%d\n", dp[W]);
}
return 0;
}