import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; public class reo { private static int n; private static int m; private static final String YES_RELATION = "T"; private static final String NO_RELATION = "N"; private static final int N = 0; private static final int T = 1; private static final String NO_MESSAGE = "NIE\n"; private static int[][] restrictions; private static int[][][] resctrictionsContradiction; public static void main(String[] args) { try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { String header = reader.readLine(); n = Integer.parseInt(header.split(" ")[0]); m = Integer.parseInt(header.split(" ")[1]); restrictions = new int[m][3]; resctrictionsContradiction = new int[n + 1][n + 1][2]; String[] currentPreferences; for (int i = 0; i < m; i++) { currentPreferences = reader.readLine().split(" "); int a = Integer.parseInt(currentPreferences[0]); int b = Integer.parseInt(currentPreferences[1]); int r = currentPreferences[2].equals(NO_RELATION) ? N : T; initializeRestriction(i, a, b, r); if (isRestrictionContradiction(a, b, r)) { System.out.println(NO_MESSAGE); return; } } for (int i = 0; i < m; i++) { int a = restrictions[i][0]; int b = restrictions[i][1]; int r = restrictions[i][2]; if (!performMove(i, a, b, r)) { System.out.println(NO_MESSAGE); return; } } int directorId = Company.getINSTANCE().getDirector().getId(); Worker[] workersArray = Company.getINSTANCE().getWorkersArray(); for (int i = 1; i < workersArray.length; i++) { if (i == directorId) { System.out.println(0); } else { System.out.println(workersArray[i].getChief().getId()); } } } catch (IOException e) { e.printStackTrace(); } } private static void initializeRestriction(int i, int a, int b, int r) { for (int j = 0; j < 3; j++) { restrictions[i][0] = a; restrictions[i][1] = b; restrictions[i][2] = r; } } private static boolean isRestrictionContradiction(int a, int b, int r) { resctrictionsContradiction[a][b][r]++; return resctrictionsContradiction[b][a][r] > 0; } private static boolean performMove(int currentRestriction, int a, int b, int r) { Worker workerA; Worker workerB; Company company = Company.getINSTANCE(); if (company.getWorkersArray()[a] != null) { workerA = company.getWorkersArray()[a]; } else { workerA = new Worker(a); } if (company.getWorkersArray()[b] != null) { workerB = company.getWorkersArray()[b]; } else { workerB = new Worker(b); } if (currentRestriction == 0) { if (r == N) { company.setDirector(workerA); workerB.setChief(workerA); workerA.getSubordinates().add(workerB); } else { company.setDirector(workerB); workerA.setChief(workerB); workerB.getSubordinates().add(workerA); } Company.getINSTANCE().getWorkersArray()[workerA.getId()] = workerA; Company.getINSTANCE().getWorkersArray()[workerB.getId()] = workerB; return true; } if (Company.getINSTANCE().getWorkersArray()[workerA.getId()] == null) { Company.getINSTANCE().getWorkersArray()[workerA.getId()] = workerA; } if (Company.getINSTANCE().getWorkersArray()[workerB.getId()] == null) { Company.getINSTANCE().getWorkersArray()[workerB.getId()] = workerB; } if (r == N) { // if (workerA.getChief() == null && !Company.getINSTANCE().getDirector().equals(workerA)) { // workerA.setChief(Company.getINSTANCE().getDirector()); // workerA.getChief().getSubordinates().add(workerA); // } // if (workerB.getChief() != null && workerB.getChief().getSubordinates() != null) { // workerB.getChief().getSubordinates().remove(workerB); // } workerB.setChief(workerA); workerA.getSubordinates().add(workerB); } else { /* if (workerB.getChief() == null && !Company.getINSTANCE().getDirector().equals(workerB)) { workerB.setChief(Company.getINSTANCE().getDirector()); workerB.getChief().getSubordinates().add(workerB); } // if (workerA.getChief() != null && workerA.getChief().getSubordinates() != null) { // workerA.getChief().getSubordinates().remove(workerA); // } if (workerA.getChief() == null && !Company.getINSTANCE().getDirector().equals(workerA)) { workerA.setChief(Company.getINSTANCE().getDirector()); company.getDirector().getSubordinates().add(workerA); }*/ } if (validateCompany(restrictions, currentRestriction)) { return true; } if (iterateCompany(Company.getINSTANCE().getDirector(), workerA, currentRestriction)) { return true; } return iterateCompany(Company.getINSTANCE().getDirector(), workerB, currentRestriction); } private static boolean iterateCompany(Worker director, Worker workerToMove, int length) { if (director.equals(workerToMove)) { return true; } if (workerToMove.getChief() != null && workerToMove.getChief().getSubordinates() != null) { workerToMove.getChief().getSubordinates().remove(workerToMove); } Worker previousChief = workerToMove.getChief(); workerToMove.setChief(director); director.getSubordinates().add(workerToMove); if (validateCompany(restrictions, length)) { return true; } workerToMove.setChief(null); director.getSubordinates().remove(workerToMove); for (Worker worker : director.getSubordinates()) { iterateCompany(worker, workerToMove, length); } if (!validateCompany(restrictions, length)) { Company.getINSTANCE().setDirector(workerToMove); iterateCompany(Company.getINSTANCE().getDirector(), director, length); } if (workerToMove.getChief() == null && !director.equals(workerToMove)) { workerToMove.setChief(director); director.getSubordinates().add(workerToMove); } return validateCompany(restrictions, length); } private static boolean validateCompany(int[][] restrictions, int length) { Worker workerA; Worker workerB; Worker tempWorker; int relation; Company company = Company.getINSTANCE(); for (int i = 0; i < length; i++) { workerA = company.getWorkersArray()[restrictions[i][0]]; workerB = company.getWorkersArray()[restrictions[i][1]]; relation = restrictions[i][2]; if (relation == N) { tempWorker = workerA; while((tempWorker = tempWorker.getChief()) != null) { if (tempWorker == workerB) { return false; } } } else { tempWorker = workerA; while((tempWorker = tempWorker.getChief()) != null) { if (tempWorker == workerB) { return true; } } return false; } } return true; } private static class Company { private Worker[] workersArray = new Worker[n + 1]; private Worker director; private static Company INSTANCE = new Company(); public static Company getINSTANCE() { return INSTANCE; } public Worker[] getWorkersArray() { return workersArray; } public Worker getDirector() { return director; } public void setDirector(Worker director) { this.director = director; } } private static class Worker { private int id; private Worker chief; private Set<Worker> subordinates = new HashSet<>(); private Set<Worker> wantedChiefs = new HashSet<>(); private Set<Worker> notWantedChiefs = new HashSet<>(); public Worker(int id) { this.id = id; } public int getId() { return id; } public Worker getChief() { return chief; } public void setChief(Worker chief) { this.chief = chief; } public Set<Worker> getSubordinates() { return subordinates; } public Set<Worker> getWantedChiefs() { return wantedChiefs; } public Set<Worker> getNotWantedChiefs() { return notWantedChiefs; } @Override public boolean equals(Object worker) { return this.getId() == ((Worker) worker).getId(); } @Override public int hashCode() { return id; } } }
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; public class reo { private static int n; private static int m; private static final String YES_RELATION = "T"; private static final String NO_RELATION = "N"; private static final int N = 0; private static final int T = 1; private static final String NO_MESSAGE = "NIE\n"; private static int[][] restrictions; private static int[][][] resctrictionsContradiction; public static void main(String[] args) { try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { String header = reader.readLine(); n = Integer.parseInt(header.split(" ")[0]); m = Integer.parseInt(header.split(" ")[1]); restrictions = new int[m][3]; resctrictionsContradiction = new int[n + 1][n + 1][2]; String[] currentPreferences; for (int i = 0; i < m; i++) { currentPreferences = reader.readLine().split(" "); int a = Integer.parseInt(currentPreferences[0]); int b = Integer.parseInt(currentPreferences[1]); int r = currentPreferences[2].equals(NO_RELATION) ? N : T; initializeRestriction(i, a, b, r); if (isRestrictionContradiction(a, b, r)) { System.out.println(NO_MESSAGE); return; } } for (int i = 0; i < m; i++) { int a = restrictions[i][0]; int b = restrictions[i][1]; int r = restrictions[i][2]; if (!performMove(i, a, b, r)) { System.out.println(NO_MESSAGE); return; } } int directorId = Company.getINSTANCE().getDirector().getId(); Worker[] workersArray = Company.getINSTANCE().getWorkersArray(); for (int i = 1; i < workersArray.length; i++) { if (i == directorId) { System.out.println(0); } else { System.out.println(workersArray[i].getChief().getId()); } } } catch (IOException e) { e.printStackTrace(); } } private static void initializeRestriction(int i, int a, int b, int r) { for (int j = 0; j < 3; j++) { restrictions[i][0] = a; restrictions[i][1] = b; restrictions[i][2] = r; } } private static boolean isRestrictionContradiction(int a, int b, int r) { resctrictionsContradiction[a][b][r]++; return resctrictionsContradiction[b][a][r] > 0; } private static boolean performMove(int currentRestriction, int a, int b, int r) { Worker workerA; Worker workerB; Company company = Company.getINSTANCE(); if (company.getWorkersArray()[a] != null) { workerA = company.getWorkersArray()[a]; } else { workerA = new Worker(a); } if (company.getWorkersArray()[b] != null) { workerB = company.getWorkersArray()[b]; } else { workerB = new Worker(b); } if (currentRestriction == 0) { if (r == N) { company.setDirector(workerA); workerB.setChief(workerA); workerA.getSubordinates().add(workerB); } else { company.setDirector(workerB); workerA.setChief(workerB); workerB.getSubordinates().add(workerA); } Company.getINSTANCE().getWorkersArray()[workerA.getId()] = workerA; Company.getINSTANCE().getWorkersArray()[workerB.getId()] = workerB; return true; } if (Company.getINSTANCE().getWorkersArray()[workerA.getId()] == null) { Company.getINSTANCE().getWorkersArray()[workerA.getId()] = workerA; } if (Company.getINSTANCE().getWorkersArray()[workerB.getId()] == null) { Company.getINSTANCE().getWorkersArray()[workerB.getId()] = workerB; } if (r == N) { // if (workerA.getChief() == null && !Company.getINSTANCE().getDirector().equals(workerA)) { // workerA.setChief(Company.getINSTANCE().getDirector()); // workerA.getChief().getSubordinates().add(workerA); // } // if (workerB.getChief() != null && workerB.getChief().getSubordinates() != null) { // workerB.getChief().getSubordinates().remove(workerB); // } workerB.setChief(workerA); workerA.getSubordinates().add(workerB); } else { /* if (workerB.getChief() == null && !Company.getINSTANCE().getDirector().equals(workerB)) { workerB.setChief(Company.getINSTANCE().getDirector()); workerB.getChief().getSubordinates().add(workerB); } // if (workerA.getChief() != null && workerA.getChief().getSubordinates() != null) { // workerA.getChief().getSubordinates().remove(workerA); // } if (workerA.getChief() == null && !Company.getINSTANCE().getDirector().equals(workerA)) { workerA.setChief(Company.getINSTANCE().getDirector()); company.getDirector().getSubordinates().add(workerA); }*/ } if (validateCompany(restrictions, currentRestriction)) { return true; } if (iterateCompany(Company.getINSTANCE().getDirector(), workerA, currentRestriction)) { return true; } return iterateCompany(Company.getINSTANCE().getDirector(), workerB, currentRestriction); } private static boolean iterateCompany(Worker director, Worker workerToMove, int length) { if (director.equals(workerToMove)) { return true; } if (workerToMove.getChief() != null && workerToMove.getChief().getSubordinates() != null) { workerToMove.getChief().getSubordinates().remove(workerToMove); } Worker previousChief = workerToMove.getChief(); workerToMove.setChief(director); director.getSubordinates().add(workerToMove); if (validateCompany(restrictions, length)) { return true; } workerToMove.setChief(null); director.getSubordinates().remove(workerToMove); for (Worker worker : director.getSubordinates()) { iterateCompany(worker, workerToMove, length); } if (!validateCompany(restrictions, length)) { Company.getINSTANCE().setDirector(workerToMove); iterateCompany(Company.getINSTANCE().getDirector(), director, length); } if (workerToMove.getChief() == null && !director.equals(workerToMove)) { workerToMove.setChief(director); director.getSubordinates().add(workerToMove); } return validateCompany(restrictions, length); } private static boolean validateCompany(int[][] restrictions, int length) { Worker workerA; Worker workerB; Worker tempWorker; int relation; Company company = Company.getINSTANCE(); for (int i = 0; i < length; i++) { workerA = company.getWorkersArray()[restrictions[i][0]]; workerB = company.getWorkersArray()[restrictions[i][1]]; relation = restrictions[i][2]; if (relation == N) { tempWorker = workerA; while((tempWorker = tempWorker.getChief()) != null) { if (tempWorker == workerB) { return false; } } } else { tempWorker = workerA; while((tempWorker = tempWorker.getChief()) != null) { if (tempWorker == workerB) { return true; } } return false; } } return true; } private static class Company { private Worker[] workersArray = new Worker[n + 1]; private Worker director; private static Company INSTANCE = new Company(); public static Company getINSTANCE() { return INSTANCE; } public Worker[] getWorkersArray() { return workersArray; } public Worker getDirector() { return director; } public void setDirector(Worker director) { this.director = director; } } private static class Worker { private int id; private Worker chief; private Set<Worker> subordinates = new HashSet<>(); private Set<Worker> wantedChiefs = new HashSet<>(); private Set<Worker> notWantedChiefs = new HashSet<>(); public Worker(int id) { this.id = id; } public int getId() { return id; } public Worker getChief() { return chief; } public void setChief(Worker chief) { this.chief = chief; } public Set<Worker> getSubordinates() { return subordinates; } public Set<Worker> getWantedChiefs() { return wantedChiefs; } public Set<Worker> getNotWantedChiefs() { return notWantedChiefs; } @Override public boolean equals(Object worker) { return this.getId() == ((Worker) worker).getId(); } @Override public int hashCode() { return id; } } } |