|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | +/** |
| 4 | + * |
| 5 | +1 |
| 6 | +5 |
| 7 | +5 4 3 2 1 |
| 8 | +3 |
| 9 | +2 4 |
| 10 | +3 4 |
| 11 | +1 5 |
| 12 | + * |
| 13 | + */ |
| 14 | +public class Main { |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException{ |
| 17 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + |
| 19 | + int t = Integer.parseInt(br.readLine()); |
| 20 | + |
| 21 | + StringBuilder sb = new StringBuilder(); |
| 22 | + for(int test=0; test<t; test++) { |
| 23 | + // init |
| 24 | + int n = Integer.parseInt(br.readLine()); |
| 25 | + |
| 26 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 27 | + |
| 28 | + Set<Integer>[] graph = new HashSet[n+1]; |
| 29 | + for(int i=1; i<=n; i++) { |
| 30 | + graph[i] = new HashSet<>(); |
| 31 | + } |
| 32 | + int[] pre = new int[n+1]; |
| 33 | + |
| 34 | + int[] nums = new int[n]; |
| 35 | + for(int i=n-1; i>=0; i--) { |
| 36 | + nums[i] = Integer.parseInt(st.nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + for(int i=0; i<n; i++) { |
| 40 | + for(int j=i+1; j<n; j++) { |
| 41 | + graph[nums[i]].add(nums[j]); |
| 42 | + pre[nums[j]]++; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // process |
| 47 | + int m = Integer.parseInt(br.readLine()); |
| 48 | + boolean flag = false; |
| 49 | + for(int i=0; i<m; i++) { |
| 50 | + st = new StringTokenizer(br.readLine()); |
| 51 | + |
| 52 | + int a = Integer.parseInt(st.nextToken()); |
| 53 | + int b = Integer.parseInt(st.nextToken()); |
| 54 | + |
| 55 | + if(graph[a].contains(b)) { |
| 56 | + graph[a].remove(b); |
| 57 | + pre[b]--; |
| 58 | + |
| 59 | + graph[b].add(a); |
| 60 | + pre[a]++; |
| 61 | + continue; |
| 62 | + } |
| 63 | + if(graph[b].contains(a)) { |
| 64 | + graph[b].remove(a); |
| 65 | + pre[a]--; |
| 66 | + |
| 67 | + graph[a].add(b); |
| 68 | + pre[b]++; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + flag = true; |
| 73 | + } |
| 74 | + |
| 75 | + // check 1 |
| 76 | + if(flag) { |
| 77 | + sb.append("IMPOSSIBLE").append("\n"); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + // make result |
| 82 | + Queue<Integer> q = new LinkedList<>(); |
| 83 | + for(int i=1; i<=n; i++) { |
| 84 | + if(pre[i] == 0) q.offer(i); |
| 85 | + } |
| 86 | + |
| 87 | + int index = 0; |
| 88 | + while(!q.isEmpty()) { |
| 89 | + if(q.size() > 1) { |
| 90 | + flag = true; |
| 91 | + break; |
| 92 | + } |
| 93 | + int current = q.poll(); |
| 94 | + |
| 95 | + nums[index] = current; |
| 96 | + |
| 97 | + for(int node : graph[current]) { |
| 98 | + pre[node]--; |
| 99 | + if(pre[node] == 0) q.offer(node); |
| 100 | + } |
| 101 | + index++; |
| 102 | + } |
| 103 | + |
| 104 | + // check 2 |
| 105 | + if(flag || index < n) { |
| 106 | + sb.append("IMPOSSIBLE").append("\n"); |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + // result |
| 111 | + sb.append(nums[n-1]); |
| 112 | + for(int i=n-2; i>=0; i--) { |
| 113 | + sb.append(" ").append(nums[i]); |
| 114 | + } |
| 115 | + sb.append("\n"); |
| 116 | + } |
| 117 | + |
| 118 | + System.out.println(sb.toString().trim()); |
| 119 | + } |
| 120 | +} |
0 commit comments