|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.InputStreamReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + static int N, K; |
| 8 | + |
| 9 | + public static void main(String[] args) throws IOException { |
| 10 | + |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + |
| 13 | + int T = Integer.parseInt(br.readLine()); |
| 14 | + StringBuilder sb = new StringBuilder(); |
| 15 | + StringTokenizer st; |
| 16 | + |
| 17 | + for (int t = 0; t < T; t++) { |
| 18 | + st = new StringTokenizer(br.readLine()); |
| 19 | + N = Integer.parseInt(st.nextToken()); |
| 20 | + K = Integer.parseInt(st.nextToken()); |
| 21 | + |
| 22 | + int[] value = new int[N]; |
| 23 | + int[] count = new int[N]; |
| 24 | + int[] answer = new int[N]; |
| 25 | + List<List<Integer>> list = new ArrayList<>(); |
| 26 | + |
| 27 | + for (int i = 0; i < N; i++) { |
| 28 | + list.add(new ArrayList<>()); |
| 29 | + } |
| 30 | + |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + for (int i = 0; i < N; i++) { |
| 33 | + value[i] = Integer.parseInt(st.nextToken()); |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = 0; i < K; i++) { |
| 37 | + st = new StringTokenizer(br.readLine()); |
| 38 | + int v = Integer.parseInt(st.nextToken()) - 1; |
| 39 | + int e = Integer.parseInt(st.nextToken()) - 1; |
| 40 | + count[e]++; |
| 41 | + list.get(v).add(e); |
| 42 | + } |
| 43 | + |
| 44 | + Queue<Integer> queue = new LinkedList<>(); |
| 45 | + for (int i = 0; i < N; i++) { |
| 46 | + if (count[i] == 0) { |
| 47 | + queue.add(i); |
| 48 | + answer[i] = value[i]; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + while (!queue.isEmpty()) { |
| 53 | + int now = queue.poll(); |
| 54 | + for (int next : list.get(now)) { |
| 55 | + count[next]--; |
| 56 | + answer[next] = Math.max(answer[next], answer[now] + value[next]); |
| 57 | + if (count[next] == 0) { |
| 58 | + queue.add(next); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + int target = Integer.parseInt(br.readLine()) - 1; |
| 64 | + sb.append(answer[target]).append("\n"); |
| 65 | + } |
| 66 | + |
| 67 | + System.out.println(sb); |
| 68 | + } |
| 69 | +} |
0 commit comments