import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Objects; import java.util.Scanner; import java.util.Set; public class kar { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { readDataAndPrintResult(scanner); } } } private static void readDataAndPrintResult(Scanner scanner) { int n = scanner.nextInt(); int m = scanner.nextInt(); Map<Pair, Result> duels = new HashMap<>(m); for (int i = 0; i < m; i++) { int a = scanner.nextInt(); char w = scanner.next().charAt(0); int b = scanner.nextInt(); if (w == '<') { duels.put(new Pair(a, b), Result.PRZEGRANA); } else { duels.put(new Pair(a, b), Result.WYGRANA); } } calculateAndPrintResult(n, duels); } private static void calculateAndPrintResult(int n, Map<Pair, Result> duels) { Set<Integer> bajtekDecks = new HashSet<>(n); Set<Integer> bitekDecks = new HashSet<>(n); for (int i = 1; i <= n; i++) { bajtekDecks.add(i); } bitekDecks.addAll(bajtekDecks); for (int i = 1; i < n; i++) { int bitekDeckToRemove; try { bitekDeckToRemove = chooseDeckToRemoveAsBajtek(bajtekDecks, bitekDecks, duels); } catch (GameWonException e) { System.out.println(Result.WYGRANA.name()); return; } bitekDecks.remove(bitekDeckToRemove); int bajtekDeckToRemove; try { bajtekDeckToRemove = chooseDeckToRemoveAsBitek(bajtekDecks, bitekDecks, duels); } catch (GameLostException e) { System.out.println(Result.PRZEGRANA.name()); return; } bajtekDecks.remove(bajtekDeckToRemove); } int lastBajektDeck = bajtekDecks.toArray(new Integer[0])[0]; int lastBitekDeck = bitekDecks.toArray(new Integer[0])[0]; System.out.println(getDuelResult(lastBajektDeck, lastBitekDeck, duels).name()); } private static int chooseDeckToRemoveAsBajtek(Set<Integer> bajtekDecks, Set<Integer> bitekDecks, Map<Pair, Result> duels) throws GameWonException { int highestLoseCounter = 0; int highestLoseCounterBitekDeck = 0; for (int bitekDeck : bitekDecks) { int loseCounter = 0; for (int bajtekDeck : bajtekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.PRZEGRANA) { loseCounter++; } } if (loseCounter > highestLoseCounter) { highestLoseCounter = loseCounter; highestLoseCounterBitekDeck = bitekDeck; } } if (highestLoseCounter > 0) { return highestLoseCounterBitekDeck; } int highestDrawCounter = 0; int highestDrawCounterBitekDeck = 0; return getHighestDrawCount(bajtekDecks, bitekDecks, duels, highestDrawCounter, highestDrawCounterBitekDeck); } private static int getHighestDrawCount(Set<Integer> bajtekDecks, Set<Integer> bitekDecks, Map<Pair, Result> duels, int highestDrawCounter, int highestDrawCounterBitekDeck) throws GameWonException { for (int bitekDeck : bitekDecks) { int drawCounter = 0; for (int bajtekDeck : bajtekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.REMIS) { drawCounter++; } } if (drawCounter > highestDrawCounterBitekDeck) { highestDrawCounter = drawCounter; highestDrawCounterBitekDeck = bitekDeck; } } if (highestDrawCounter > 0) { return highestDrawCounterBitekDeck; } throw new GameWonException(); } private static int chooseDeckToRemoveAsBitek(Set<Integer> bajtekDecks, Set<Integer> bitekDecks, Map<Pair, Result> duels) throws GameLostException { int highestLoseCounter = 0; int highestLoseCounterBajtekDeck = 0; for (int bajtekDeck : bajtekDecks) { int loseCounter = 0; for (int bitekDeck : bitekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.WYGRANA) { loseCounter++; } } if (loseCounter > highestLoseCounter) { highestLoseCounter = loseCounter; highestLoseCounterBajtekDeck = bajtekDeck; } } if (highestLoseCounter > 0) { return highestLoseCounterBajtekDeck; } int highestDrawCounter = 0; int highestDrawCounterBitekDeck = 0; for (int bajtekDeck : bajtekDecks) { int drawCounter = 0; for (int bitekDeck : bitekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.REMIS) { drawCounter++; } } if (drawCounter > highestDrawCounterBitekDeck) { highestDrawCounter = drawCounter; highestDrawCounterBitekDeck = bajtekDeck; } } if (highestDrawCounter > 0) { return highestDrawCounterBitekDeck; } throw new GameLostException(); } private static Result getDuelResult(int a, int b, Map<Pair, Result> duels) { Result result = duels.get(new Pair(a, b)); if (result == null) { return Result.REMIS; } return result; } private enum Result { WYGRANA, REMIS, PRZEGRANA } private static class Pair { private final int a; private final int b; Pair(int a, int b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { return Objects.hash(a, b); } } private static class GameWonException extends Exception {} private static class GameLostException extends Exception {} }
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Objects; import java.util.Scanner; import java.util.Set; public class kar { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int t = scanner.nextInt(); for (int i = 0; i < t; i++) { readDataAndPrintResult(scanner); } } } private static void readDataAndPrintResult(Scanner scanner) { int n = scanner.nextInt(); int m = scanner.nextInt(); Map<Pair, Result> duels = new HashMap<>(m); for (int i = 0; i < m; i++) { int a = scanner.nextInt(); char w = scanner.next().charAt(0); int b = scanner.nextInt(); if (w == '<') { duels.put(new Pair(a, b), Result.PRZEGRANA); } else { duels.put(new Pair(a, b), Result.WYGRANA); } } calculateAndPrintResult(n, duels); } private static void calculateAndPrintResult(int n, Map<Pair, Result> duels) { Set<Integer> bajtekDecks = new HashSet<>(n); Set<Integer> bitekDecks = new HashSet<>(n); for (int i = 1; i <= n; i++) { bajtekDecks.add(i); } bitekDecks.addAll(bajtekDecks); for (int i = 1; i < n; i++) { int bitekDeckToRemove; try { bitekDeckToRemove = chooseDeckToRemoveAsBajtek(bajtekDecks, bitekDecks, duels); } catch (GameWonException e) { System.out.println(Result.WYGRANA.name()); return; } bitekDecks.remove(bitekDeckToRemove); int bajtekDeckToRemove; try { bajtekDeckToRemove = chooseDeckToRemoveAsBitek(bajtekDecks, bitekDecks, duels); } catch (GameLostException e) { System.out.println(Result.PRZEGRANA.name()); return; } bajtekDecks.remove(bajtekDeckToRemove); } int lastBajektDeck = bajtekDecks.toArray(new Integer[0])[0]; int lastBitekDeck = bitekDecks.toArray(new Integer[0])[0]; System.out.println(getDuelResult(lastBajektDeck, lastBitekDeck, duels).name()); } private static int chooseDeckToRemoveAsBajtek(Set<Integer> bajtekDecks, Set<Integer> bitekDecks, Map<Pair, Result> duels) throws GameWonException { int highestLoseCounter = 0; int highestLoseCounterBitekDeck = 0; for (int bitekDeck : bitekDecks) { int loseCounter = 0; for (int bajtekDeck : bajtekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.PRZEGRANA) { loseCounter++; } } if (loseCounter > highestLoseCounter) { highestLoseCounter = loseCounter; highestLoseCounterBitekDeck = bitekDeck; } } if (highestLoseCounter > 0) { return highestLoseCounterBitekDeck; } int highestDrawCounter = 0; int highestDrawCounterBitekDeck = 0; return getHighestDrawCount(bajtekDecks, bitekDecks, duels, highestDrawCounter, highestDrawCounterBitekDeck); } private static int getHighestDrawCount(Set<Integer> bajtekDecks, Set<Integer> bitekDecks, Map<Pair, Result> duels, int highestDrawCounter, int highestDrawCounterBitekDeck) throws GameWonException { for (int bitekDeck : bitekDecks) { int drawCounter = 0; for (int bajtekDeck : bajtekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.REMIS) { drawCounter++; } } if (drawCounter > highestDrawCounterBitekDeck) { highestDrawCounter = drawCounter; highestDrawCounterBitekDeck = bitekDeck; } } if (highestDrawCounter > 0) { return highestDrawCounterBitekDeck; } throw new GameWonException(); } private static int chooseDeckToRemoveAsBitek(Set<Integer> bajtekDecks, Set<Integer> bitekDecks, Map<Pair, Result> duels) throws GameLostException { int highestLoseCounter = 0; int highestLoseCounterBajtekDeck = 0; for (int bajtekDeck : bajtekDecks) { int loseCounter = 0; for (int bitekDeck : bitekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.WYGRANA) { loseCounter++; } } if (loseCounter > highestLoseCounter) { highestLoseCounter = loseCounter; highestLoseCounterBajtekDeck = bajtekDeck; } } if (highestLoseCounter > 0) { return highestLoseCounterBajtekDeck; } int highestDrawCounter = 0; int highestDrawCounterBitekDeck = 0; for (int bajtekDeck : bajtekDecks) { int drawCounter = 0; for (int bitekDeck : bitekDecks) { Result duelResult = getDuelResult(bajtekDeck, bitekDeck, duels); if (duelResult == Result.REMIS) { drawCounter++; } } if (drawCounter > highestDrawCounterBitekDeck) { highestDrawCounter = drawCounter; highestDrawCounterBitekDeck = bajtekDeck; } } if (highestDrawCounter > 0) { return highestDrawCounterBitekDeck; } throw new GameLostException(); } private static Result getDuelResult(int a, int b, Map<Pair, Result> duels) { Result result = duels.get(new Pair(a, b)); if (result == null) { return Result.REMIS; } return result; } private enum Result { WYGRANA, REMIS, PRZEGRANA } private static class Pair { private final int a; private final int b; Pair(int a, int b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; Pair pair = (Pair) o; return a == pair.a && b == pair.b; } @Override public int hashCode() { return Objects.hash(a, b); } } private static class GameWonException extends Exception {} private static class GameLostException extends Exception {} } |