import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.StringTokenizer; public class kar { /** code from https://www.cpe.ku.ac.th/~jim/java-io.html * Class for buffered reading int and double values */ private static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static DECK_STRENGTH nextSign() throws IOException { String n = next(); if (n.equals("<")) { return DECK_STRENGTH.WEAKER; } else { return DECK_STRENGTH.BETTER; } } } public enum DECK_STRENGTH { WEAKER, BETTER } public static class Graph { public Graph(int nDecks) { this.nDecks = nDecks; left = new ArrayList[nDecks]; right = new ArrayList[nDecks]; leftDegrees = new int[nDecks]; rightDegrees = new int[nDecks]; Arrays.fill(leftDegrees, 0); Arrays.fill(rightDegrees, 0); for (int i=0; i< nDecks; i++) { left[i] = new ArrayList<>(); right[i] = new ArrayList<>(); } } private int nDecks; private ArrayList<Integer>[] left; private ArrayList<Integer>[] right; private int[] leftDegrees; private int[] rightDegrees; public static class Node { public int id; public Node(int nodeId) { id = nodeId; } @Override public boolean equals(Object o) { Node n = (Node)o; return id == n.id; } } PriorityQueue<Node> maxDegreeLeft = new PriorityQueue<>(new Comparator<Node>(){ @Override public int compare(Node o1, Node o2) { return Integer.compare(leftDegrees[o1.id], leftDegrees[o2.id]); } }); PriorityQueue<Node> minDegreeRight = new PriorityQueue<>(new Comparator<Node>(){ @Override public int compare(Node o1, Node o2) { return - Integer.compare(rightDegrees[o1.id], rightDegrees[o2.id]); } }); public void add(int myDeckId, int hisDeckId) { left[myDeckId].add(hisDeckId); right[hisDeckId].add(myDeckId); leftDegrees[myDeckId]++; rightDegrees[hisDeckId]++; } public boolean isKConnected(int k) { if (maxDegree(rightDegrees)) { return true; } else { for (int i =0; i<nDecks; i++) { maxDegreeLeft.add(new Node(i)); minDegreeRight.add(new Node(i)); } while (minDegreeRight.size() > 1 && maxDegreeLeft.size() > 1) { Node rNode = minDegreeRight.poll(); for(int leftNodeId : right[rNode.id]) { leftDegrees[leftNodeId]--; decreaseKey(maxDegreeLeft,leftNodeId); } Node lNode = maxDegreeLeft.poll(); for(int rightNodeId : left[lNode.id]) { rightDegrees[rightNodeId]--; decreaseKey(minDegreeRight,rightNodeId); } } Node rNode = minDegreeRight.poll(); Node lNode = maxDegreeLeft.poll(); return right[lNode.id].contains(rNode.id); } } private void decreaseKey(PriorityQueue<Node> q,int leftNodeId) { Node tmp = new Node(leftNodeId); q.remove(tmp); q.add(tmp); } private boolean minDegree(int[] rightDegrees2) { int m = Integer.MAX_VALUE; for(int degree : rightDegrees) { m = Math.min(m, degree); } return m >= nDecks-1; } private boolean maxDegree(int[] rightDegrees) { int m = Integer.MIN_VALUE; for(int degree : rightDegrees) { m = Math.max(m, degree); } return m == nDecks; } } public static void main(String[] args) throws IOException { Reader.init(System.in); int nTestCases = Reader.nextInt(); for(int i=0; i<nTestCases; i++) { int nDecks = Reader.nextInt(); int nPairs = Reader.nextInt(); Graph myWins = new Graph(nDecks); Graph hisWins = new Graph(nDecks); for(int j=0; j<nPairs; j++) { int myDeckId = Reader.nextInt() - 1; DECK_STRENGTH strength = Reader.nextSign(); int hisDeckId = Reader.nextInt() - 1; if (strength == DECK_STRENGTH.BETTER) { myWins.add(myDeckId,hisDeckId); } else { hisWins.add(hisDeckId,myDeckId); } } if (myWins.isKConnected(nDecks-1)) { System.out.println("WYGRANA"); } else if (hisWins.isKConnected(nDecks-1)) { System.out.println("PRZEGRANA"); } else { System.out.println("REMIS"); } } } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.StringTokenizer; public class kar { /** code from https://www.cpe.ku.ac.th/~jim/java-io.html * Class for buffered reading int and double values */ private static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static DECK_STRENGTH nextSign() throws IOException { String n = next(); if (n.equals("<")) { return DECK_STRENGTH.WEAKER; } else { return DECK_STRENGTH.BETTER; } } } public enum DECK_STRENGTH { WEAKER, BETTER } public static class Graph { public Graph(int nDecks) { this.nDecks = nDecks; left = new ArrayList[nDecks]; right = new ArrayList[nDecks]; leftDegrees = new int[nDecks]; rightDegrees = new int[nDecks]; Arrays.fill(leftDegrees, 0); Arrays.fill(rightDegrees, 0); for (int i=0; i< nDecks; i++) { left[i] = new ArrayList<>(); right[i] = new ArrayList<>(); } } private int nDecks; private ArrayList<Integer>[] left; private ArrayList<Integer>[] right; private int[] leftDegrees; private int[] rightDegrees; public static class Node { public int id; public Node(int nodeId) { id = nodeId; } @Override public boolean equals(Object o) { Node n = (Node)o; return id == n.id; } } PriorityQueue<Node> maxDegreeLeft = new PriorityQueue<>(new Comparator<Node>(){ @Override public int compare(Node o1, Node o2) { return Integer.compare(leftDegrees[o1.id], leftDegrees[o2.id]); } }); PriorityQueue<Node> minDegreeRight = new PriorityQueue<>(new Comparator<Node>(){ @Override public int compare(Node o1, Node o2) { return - Integer.compare(rightDegrees[o1.id], rightDegrees[o2.id]); } }); public void add(int myDeckId, int hisDeckId) { left[myDeckId].add(hisDeckId); right[hisDeckId].add(myDeckId); leftDegrees[myDeckId]++; rightDegrees[hisDeckId]++; } public boolean isKConnected(int k) { if (maxDegree(rightDegrees)) { return true; } else { for (int i =0; i<nDecks; i++) { maxDegreeLeft.add(new Node(i)); minDegreeRight.add(new Node(i)); } while (minDegreeRight.size() > 1 && maxDegreeLeft.size() > 1) { Node rNode = minDegreeRight.poll(); for(int leftNodeId : right[rNode.id]) { leftDegrees[leftNodeId]--; decreaseKey(maxDegreeLeft,leftNodeId); } Node lNode = maxDegreeLeft.poll(); for(int rightNodeId : left[lNode.id]) { rightDegrees[rightNodeId]--; decreaseKey(minDegreeRight,rightNodeId); } } Node rNode = minDegreeRight.poll(); Node lNode = maxDegreeLeft.poll(); return right[lNode.id].contains(rNode.id); } } private void decreaseKey(PriorityQueue<Node> q,int leftNodeId) { Node tmp = new Node(leftNodeId); q.remove(tmp); q.add(tmp); } private boolean minDegree(int[] rightDegrees2) { int m = Integer.MAX_VALUE; for(int degree : rightDegrees) { m = Math.min(m, degree); } return m >= nDecks-1; } private boolean maxDegree(int[] rightDegrees) { int m = Integer.MIN_VALUE; for(int degree : rightDegrees) { m = Math.max(m, degree); } return m == nDecks; } } public static void main(String[] args) throws IOException { Reader.init(System.in); int nTestCases = Reader.nextInt(); for(int i=0; i<nTestCases; i++) { int nDecks = Reader.nextInt(); int nPairs = Reader.nextInt(); Graph myWins = new Graph(nDecks); Graph hisWins = new Graph(nDecks); for(int j=0; j<nPairs; j++) { int myDeckId = Reader.nextInt() - 1; DECK_STRENGTH strength = Reader.nextSign(); int hisDeckId = Reader.nextInt() - 1; if (strength == DECK_STRENGTH.BETTER) { myWins.add(myDeckId,hisDeckId); } else { hisWins.add(hisDeckId,myDeckId); } } if (myWins.isKConnected(nDecks-1)) { System.out.println("WYGRANA"); } else if (hisWins.isKConnected(nDecks-1)) { System.out.println("PRZEGRANA"); } else { System.out.println("REMIS"); } } } } |