import java.io.*; import java.util.*; public class kar { static final int MAX_M = 200000; public static class IN { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; private void prepare() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } public int getInt() throws IOException { prepare(); return Integer.parseInt(st.nextToken()); } public long getLong() throws IOException { prepare(); return Long.parseLong(st.nextToken()); } public String getString() throws IOException { prepare(); return st.nextToken(); } } public static class MinStorage { int n; int maxN; public MinStorage(int n) { this.n = n; } } public static class AttackGraphSide { HashMap<Integer, HashSet<Integer>> isAttacking; HashMap<Integer, HashSet<Integer>> isAttackedBy; PriorityQueue<Integer> myLeastAttacked; public AttackGraphSide(int n) { isAttacking = new HashMap<>(n); isAttackedBy = new HashMap<>(n); for (int i=0;i<n; i++) { isAttackedBy.put(i, new HashSet<>()); isAttacking.put(i, new HashSet<>()); } myLeastAttacked = new PriorityQueue<>((o1, o2) -> isAttackedBy.get(o1).size() - isAttackedBy.get(o2).size() // TODO validate ); } public void setIsAttacking(int attacker, int attacked) { isAttacking.get(attacker).add(attacked); } public void setIsAttacked(int attacker, int attacked) { isAttackedBy.get(attacked).add(attacker); } public int leastAttackedOponent(AttackGraphSide X) { int minAttacks = Integer.MAX_VALUE; int minNode = -1; int emptyNode = -1; int minEmptyNode = Integer.MAX_VALUE; for (Map.Entry<Integer, HashSet<Integer>> e: X.isAttackedBy.entrySet()) { int node = e.getKey(); int size = e.getValue().size(); if (size < minAttacks && X.isAttacking.get(node).size() > 0) { minAttacks = e.getValue().size(); minNode = node; } else if (size < minEmptyNode) { minEmptyNode = size; emptyNode = node; } } if (minNode == -1) return emptyNode; return minNode; } public void removeMy(int a, AttackGraphSide X) { X.removeOponent(a, this); isAttacking.remove(a); isAttackedBy.remove(a); } public void removeOponent(int b, AttackGraphSide X) { for (Integer attacked : X.isAttacking.get(b)) { isAttackedBy.get(attacked).remove(b); } for (Integer attacking : X.isAttackedBy.get(b)) { isAttacking.get(attacking).remove(b); } } public boolean hasWon() { return !isAttacking.values().iterator().next().isEmpty(); } public void debug() { System.out.println("Attacking:"); System.out.println(isAttacking); System.out.println("Attacked:"); System.out.println(isAttackedBy); } } public static class Game { AttackGraphSide A, B; int n; public Game(int n) { this.n = n; A = new AttackGraphSide(n); B = new AttackGraphSide(n); } public void AwinsB(int a, int b) { A.setIsAttacking(a, b); B.setIsAttacked(a, b); } public void BwinsA(int a, int b) { B.setIsAttacking(b, a); A.setIsAttacked(b, a); } public String play() { // A.debug(); // B.debug(); while (--n > 0) { int b = A.leastAttackedOponent(B); B.removeMy(b, A); int a = B.leastAttackedOponent(A); A.removeMy(a, B); } if (A.hasWon()) { return "WYGRANA"; } if (B.hasWon()) { return "PRZEGRANA"; } return "REMIS"; } } public static void solve(IN in) throws IOException { int n = in.getInt(); int m = in.getInt(); Game g = new Game(n); for (int i=0;i<m;i++) { int a = in.getInt() - 1; String c = in.getString(); int b = in.getInt() - 1; if ("<".equals(c)) { g.BwinsA(a, b); } else { g.AwinsB(a, b); } } System.out.println(g.play()); } public static void main(String[] args) throws IOException { IN in = new IN(); int t = in.getInt(); while (t-- > 0 ) { solve(in); } } }
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import java.io.*; import java.util.*; public class kar { static final int MAX_M = 200000; public static class IN { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; private void prepare() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } public int getInt() throws IOException { prepare(); return Integer.parseInt(st.nextToken()); } public long getLong() throws IOException { prepare(); return Long.parseLong(st.nextToken()); } public String getString() throws IOException { prepare(); return st.nextToken(); } } public static class MinStorage { int n; int maxN; public MinStorage(int n) { this.n = n; } } public static class AttackGraphSide { HashMap<Integer, HashSet<Integer>> isAttacking; HashMap<Integer, HashSet<Integer>> isAttackedBy; PriorityQueue<Integer> myLeastAttacked; public AttackGraphSide(int n) { isAttacking = new HashMap<>(n); isAttackedBy = new HashMap<>(n); for (int i=0;i<n; i++) { isAttackedBy.put(i, new HashSet<>()); isAttacking.put(i, new HashSet<>()); } myLeastAttacked = new PriorityQueue<>((o1, o2) -> isAttackedBy.get(o1).size() - isAttackedBy.get(o2).size() // TODO validate ); } public void setIsAttacking(int attacker, int attacked) { isAttacking.get(attacker).add(attacked); } public void setIsAttacked(int attacker, int attacked) { isAttackedBy.get(attacked).add(attacker); } public int leastAttackedOponent(AttackGraphSide X) { int minAttacks = Integer.MAX_VALUE; int minNode = -1; int emptyNode = -1; int minEmptyNode = Integer.MAX_VALUE; for (Map.Entry<Integer, HashSet<Integer>> e: X.isAttackedBy.entrySet()) { int node = e.getKey(); int size = e.getValue().size(); if (size < minAttacks && X.isAttacking.get(node).size() > 0) { minAttacks = e.getValue().size(); minNode = node; } else if (size < minEmptyNode) { minEmptyNode = size; emptyNode = node; } } if (minNode == -1) return emptyNode; return minNode; } public void removeMy(int a, AttackGraphSide X) { X.removeOponent(a, this); isAttacking.remove(a); isAttackedBy.remove(a); } public void removeOponent(int b, AttackGraphSide X) { for (Integer attacked : X.isAttacking.get(b)) { isAttackedBy.get(attacked).remove(b); } for (Integer attacking : X.isAttackedBy.get(b)) { isAttacking.get(attacking).remove(b); } } public boolean hasWon() { return !isAttacking.values().iterator().next().isEmpty(); } public void debug() { System.out.println("Attacking:"); System.out.println(isAttacking); System.out.println("Attacked:"); System.out.println(isAttackedBy); } } public static class Game { AttackGraphSide A, B; int n; public Game(int n) { this.n = n; A = new AttackGraphSide(n); B = new AttackGraphSide(n); } public void AwinsB(int a, int b) { A.setIsAttacking(a, b); B.setIsAttacked(a, b); } public void BwinsA(int a, int b) { B.setIsAttacking(b, a); A.setIsAttacked(b, a); } public String play() { // A.debug(); // B.debug(); while (--n > 0) { int b = A.leastAttackedOponent(B); B.removeMy(b, A); int a = B.leastAttackedOponent(A); A.removeMy(a, B); } if (A.hasWon()) { return "WYGRANA"; } if (B.hasWon()) { return "PRZEGRANA"; } return "REMIS"; } } public static void solve(IN in) throws IOException { int n = in.getInt(); int m = in.getInt(); Game g = new Game(n); for (int i=0;i<m;i++) { int a = in.getInt() - 1; String c = in.getString(); int b = in.getInt() - 1; if ("<".equals(c)) { g.BwinsA(a, b); } else { g.AwinsB(a, b); } } System.out.println(g.play()); } public static void main(String[] args) throws IOException { IN in = new IN(); int t = in.getInt(); while (t-- > 0 ) { solve(in); } } } |