-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1005.cpp
More file actions
65 lines (55 loc) · 1.05 KB
/
BOJ_1005.cpp
File metadata and controls
65 lines (55 loc) · 1.05 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
53
54
55
56
57
58
59
60
61
62
63
64
65
// 21/11/01
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> building(1001, 0);
vector<vector<int>> seq(1001, vector<int>(0, 0));
vector<int> dp(1001, 0);
int n, k, target;
void Input(void)
{
for (int i=1; i<=n; i++)
{
cin >> building[i];
seq[i].clear();
dp[i] = -1;
}
}
void Input_seq(void)
{
while (k--)
{
int a, b;
cin >> a >> b;
seq[b].push_back(a);
}
}
int Solve(int num)
{
if (dp[num]!=-1)
return dp[num];
vector<int> arr;
for (int i=0; i<(int)seq[num].size(); i++)
arr.push_back(Solve(seq[num][i]));
if (arr.empty())
return dp[num] = building[num];
else
return dp[num] = building[num]+*max_element(arr.begin(), arr.end());
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
cin >> n >> k;
Input();
Input_seq();
cin >> target;
cout << Solve(target) <<"\n";
}
return 0;
}