// Use my Floyd-Warshall solution to see if there are any cyclic "is-a" // dependencies import java.util.*; public class cycle { public static Scanner in; public static int n, // number of relationships m; // number of queries public static int count; // number of distinct names public static boolean isa[][], hasa[][]; // the relations public static HashMap names; // for fast lookup of names public static ArrayList nms; // names in order of appearance public static ArrayList rels; // inputs after names have been processed public static void main(String[] args) { in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); names = new HashMap(); nms = new ArrayList(); rels = new ArrayList(); count = 0; for (int i = 0; i < n; i++) { String x = in.next(); String rel = in.next(); String y = in.next(); int xk, yk; if (names.containsKey(x)) { xk = names.get(x); } else { xk = count++; names.put(x,xk); nms.add(x); //System.out.println("Adding name " + x + " in position " + xk); } if (names.containsKey(y)) { yk = names.get(y); } else { yk = count++; names.put(y,yk); nms.add(y); //System.out.println("Adding name " + y + " in position " + yk); } rels.add(""+xk+" "+rel+" "+yk); } isa = new boolean[count][count]; for (int i = 0; i < count; i++) { isa[i] = new boolean[count]; Arrays.fill(isa[i],false); isa[i][i] = true; } hasa = new boolean[count][count]; for (int i = 0; i < count; i++) { hasa[i] = new boolean[count]; Arrays.fill(hasa[i],false); } for (int i = 0; i < n; i++) { Scanner in2 = new Scanner(rels.get(i)); int xk = in2.nextInt(); String rel = in2.next(); int yk = in2.nextInt(); if (rel.equals("is-a")) { isa[xk][yk] = true; } else if (rel.equals("has-a")) { hasa[xk][yk] = true; } else {System.out.println("ERROR IN INPUT: BAD RELATION "+rel); System.exit(0);} } closeIsa(); for (int i = 0; i < count-1; i++) { for (int j = i+1; j < count; j++) { if (isa[i][j] && isa[j][i]) { System.out.println("Cyclic is-a for "+nms.get(i)+", "+nms.get(j)); System.exit(0); } } } System.exit(42); // REST OF "main" COMMENTED OUT SINCE NOT NEEDED FOR THE is-a CYCLE CHECK! /* closeHasa(); for (int i = 0; i < m; i++) { String x = in.next(); String rel = in.next(); String y = in.next(); int xk = -1, yk = -1; if (names.containsKey(x)) { xk = names.get(x); } else { System.out.println("Error--no such class "+x); } if (names.containsKey(y)) { yk = names.get(y); } else { System.out.println("Error--no such class "+y); } System.out.print ("Query "+(i+1)+": "); if (rel.equals("is-a")) { System.out.println(isa[xk][yk]); } else { System.out.println(hasa[xk][yk]); } } */ } public static void closeIsa() { for (int k = 0; k < count; k++) { for (int i = 0; i < count; i++) { for (int j = 0; j < count; j++) { isa[i][j] = isa[i][j] || (isa[i][k] && isa[k][j]); } } } } /* public static void closeHasa() { for (int k = 0; k < count; k++) { for (int i = 0; i < count; i++) { for (int j = 0; j < count; j++) { hasa[i][j] = hasa[i][j] || (hasa[i][k] && hasa[k][j]); hasa[i][j] = hasa[i][j] || (isa[i][k] && hasa[k][j]); hasa[i][j] = hasa[i][j] || (hasa[i][k] && isa[k][j]); } } } } /* /* For debugging: */ /* public static void displayHasa() { System.out.println("Has-A:"); for (int i = 0; i < count; i++) { for (int j = 0; j < count; j++) { if (hasa[i][j]) { System.out.println(nms.get(i)+" has-a "+nms.get(j)); } } } } public static void displayIsa() { System.out.println("Is-A:"); for (int i = 0; i < count; i++) { for (int j = 0; j < count; j++) { if (isa[i][j]) { System.out.println(nms.get(i)+" is-a "+nms.get(j)); } } } } */ }