import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; /** * Created by gmatuszewski on 22.11.2016. */ public class kar { private static StreamTokenizer ST; enum CMP { LT, GT } enum RESULT { WYGRANA, PRZEGRANA, REMIS } public static void main(String[] args) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { ST = new StreamTokenizer(br); int n = nextInt(); while (n-- > 0){ System.out.println(performMatch().name()); } } } private static RESULT performMatch() throws IOException { int n = nextInt(); int m = nextInt(); Map<Integer, Set<Integer>> aBetter = new HashMap<>(); Map<Integer, Set<Integer>> aWorse = new HashMap<>(); Map<Integer, Set<Integer>> bBetter = new HashMap<>(); Map<Integer, Set<Integer>> bWorse = new HashMap<>(); Set<Integer> aNeutral = IntStream.range(1, n+1).boxed().collect(Collectors.toSet()); Set<Integer> bNeutral = IntStream.range(1, n+1).boxed().collect(Collectors.toSet()); { int a, b; CMP cmp; while (m-- > 0) { a = nextInt(); cmp = nextCMP(); b = nextInt(); if (cmp == CMP.LT) { aWorse.computeIfAbsent(a, i -> new HashSet<>()).add(b); bBetter.computeIfAbsent(b, i -> new HashSet<>()).add(a); } else { aBetter.computeIfAbsent(a, i -> new HashSet<>()).add(b); bWorse.computeIfAbsent(b, i -> new HashSet<>()).add(a); } } } aNeutral.removeAll(aBetter.keySet()); aNeutral.removeAll(aWorse.keySet()); bNeutral.removeAll(bBetter.keySet()); bNeutral.removeAll(bWorse.keySet()); Set<Integer> newNeutrals = new HashSet<>(); for (int i = 2*(n-1); i > 0; i--){ Map<Integer, Set<Integer>> fBetter = i % 2 == 0 ? aBetter : bBetter; Map<Integer, Set<Integer>> fWorse = i % 2 == 0 ? aWorse : bWorse; Map<Integer, Set<Integer>> sBetter = i % 2 == 0 ? bBetter : aBetter; Map<Integer, Set<Integer>> sWorse = i % 2 == 0 ? bWorse : aWorse; Set<Integer> fNeutral = i % 2 == 0 ? aNeutral : bNeutral; Set<Integer> sNeutral = i % 2 == 0 ? bNeutral : aNeutral; newNeutrals.clear(); Integer s; if (sBetter.isEmpty() == false){ s = sBetter.keySet().iterator().next(); } else if (sNeutral.isEmpty() == false){ s = sNeutral.iterator().next(); } else { s = sWorse.keySet().iterator().next(); } sNeutral.remove(s); filter(s, sBetter, fWorse, newNeutrals); filter(s, sWorse, fBetter, newNeutrals); for (Integer neutral : newNeutrals) { if (fWorse.get(neutral) == null && fBetter.get(neutral) == null) fNeutral.add(neutral); } } if (aBetter.isEmpty() == false) return RESULT.WYGRANA; if (aWorse.isEmpty() == false) return RESULT.PRZEGRANA; return RESULT.REMIS; } private static void filter(Integer s, Map<Integer, Set<Integer>> second, Map<Integer, Set<Integer>> first, Set<Integer> fNeutral) { Set<Integer> remove = second.remove(s); if (remove != null){ for (Integer f : remove) { Set<Integer> secs = first.get(f); if (secs != null){ secs.remove(s); if (secs.isEmpty()){ first.remove(f); fNeutral.add(f); } } } } } static int nextInt() throws IOException { while (ST.nextToken() != StreamTokenizer.TT_NUMBER){} return (int) ST.nval; } static CMP nextCMP() throws IOException { int token; while (true){ token = ST.nextToken(); if (token == '>') return CMP.GT; else if (token == '<') return CMP.LT; } } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; /** * Created by gmatuszewski on 22.11.2016. */ public class kar { private static StreamTokenizer ST; enum CMP { LT, GT } enum RESULT { WYGRANA, PRZEGRANA, REMIS } public static void main(String[] args) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { ST = new StreamTokenizer(br); int n = nextInt(); while (n-- > 0){ System.out.println(performMatch().name()); } } } private static RESULT performMatch() throws IOException { int n = nextInt(); int m = nextInt(); Map<Integer, Set<Integer>> aBetter = new HashMap<>(); Map<Integer, Set<Integer>> aWorse = new HashMap<>(); Map<Integer, Set<Integer>> bBetter = new HashMap<>(); Map<Integer, Set<Integer>> bWorse = new HashMap<>(); Set<Integer> aNeutral = IntStream.range(1, n+1).boxed().collect(Collectors.toSet()); Set<Integer> bNeutral = IntStream.range(1, n+1).boxed().collect(Collectors.toSet()); { int a, b; CMP cmp; while (m-- > 0) { a = nextInt(); cmp = nextCMP(); b = nextInt(); if (cmp == CMP.LT) { aWorse.computeIfAbsent(a, i -> new HashSet<>()).add(b); bBetter.computeIfAbsent(b, i -> new HashSet<>()).add(a); } else { aBetter.computeIfAbsent(a, i -> new HashSet<>()).add(b); bWorse.computeIfAbsent(b, i -> new HashSet<>()).add(a); } } } aNeutral.removeAll(aBetter.keySet()); aNeutral.removeAll(aWorse.keySet()); bNeutral.removeAll(bBetter.keySet()); bNeutral.removeAll(bWorse.keySet()); Set<Integer> newNeutrals = new HashSet<>(); for (int i = 2*(n-1); i > 0; i--){ Map<Integer, Set<Integer>> fBetter = i % 2 == 0 ? aBetter : bBetter; Map<Integer, Set<Integer>> fWorse = i % 2 == 0 ? aWorse : bWorse; Map<Integer, Set<Integer>> sBetter = i % 2 == 0 ? bBetter : aBetter; Map<Integer, Set<Integer>> sWorse = i % 2 == 0 ? bWorse : aWorse; Set<Integer> fNeutral = i % 2 == 0 ? aNeutral : bNeutral; Set<Integer> sNeutral = i % 2 == 0 ? bNeutral : aNeutral; newNeutrals.clear(); Integer s; if (sBetter.isEmpty() == false){ s = sBetter.keySet().iterator().next(); } else if (sNeutral.isEmpty() == false){ s = sNeutral.iterator().next(); } else { s = sWorse.keySet().iterator().next(); } sNeutral.remove(s); filter(s, sBetter, fWorse, newNeutrals); filter(s, sWorse, fBetter, newNeutrals); for (Integer neutral : newNeutrals) { if (fWorse.get(neutral) == null && fBetter.get(neutral) == null) fNeutral.add(neutral); } } if (aBetter.isEmpty() == false) return RESULT.WYGRANA; if (aWorse.isEmpty() == false) return RESULT.PRZEGRANA; return RESULT.REMIS; } private static void filter(Integer s, Map<Integer, Set<Integer>> second, Map<Integer, Set<Integer>> first, Set<Integer> fNeutral) { Set<Integer> remove = second.remove(s); if (remove != null){ for (Integer f : remove) { Set<Integer> secs = first.get(f); if (secs != null){ secs.remove(s); if (secs.isEmpty()){ first.remove(f); fNeutral.add(f); } } } } } static int nextInt() throws IOException { while (ST.nextToken() != StreamTokenizer.TT_NUMBER){} return (int) ST.nval; } static CMP nextCMP() throws IOException { int token; while (true){ token = ST.nextToken(); if (token == '>') return CMP.GT; else if (token == '<') return CMP.LT; } } } |