fork download
  1. import java.io.*;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6. public class Main{
  7. public static final int mod = 1000000007;
  8. public static HashMap<String,Integer> map ;
  9. public static int count ;
  10. public static ArrayList<ArrayList<Integer>> adj;
  11. public static boolean visited[],recstack[];
  12. public static void main(String[] args)throws IOException {
  13. // TODO Auto-generated method stub
  14. InputStream input = System.in;
  15. InputReader in = new InputReader(input);
  16.  
  17. //File file = new File("C:\\Users\\SAWANT\\Downloads\\C-small-attempt0 (1).in");
  18.  
  19. // BufferedReader br = new BufferedReader(new FileReader(file));
  20. int t = Integer.parseInt(br.readLine());
  21.  
  22. for(int o=1; o<=t; o++)
  23. {
  24. int n = Integer.parseInt(br.readLine());
  25.  
  26. map = new HashMap<String,Integer>();
  27. adj = new ArrayList<ArrayList<Integer>>(); //basically a 2-D array for storing adj.
  28. int c = 1; // dummy node count
  29. adj.add(new ArrayList<Integer>()); // dummy node
  30. count = 0; //visited node count in dfs
  31. for(int i=1; i<=n; i++)
  32. {
  33. String s = br.readLine();
  34.  
  35. String var = s.substring(0, s.indexOf('='));
  36. int varindex = -1;
  37. if(map.containsKey(var))
  38. {
  39. varindex = map.get(var);
  40.  
  41. }
  42. else
  43. {
  44. varindex= c++;
  45.  
  46. map.put(var,varindex);
  47.  
  48. adj.add(new ArrayList<Integer>());
  49. }
  50. String ss = s.substring(s.indexOf('(') +1,s.indexOf(')'));
  51.  
  52. if(ss.equals(""))
  53. {
  54. adj.get(0).add(varindex);continue;
  55. }
  56.  
  57. StringTokenizer st = new StringTokenizer(ss,",");
  58.  
  59. while(st.hasMoreTokens())
  60. {
  61. String next = st.nextToken();int argindex = -1;
  62.  
  63. if(!map.containsKey(next))
  64. {
  65. argindex = c++;
  66. map.put(next,argindex);
  67.  
  68. adj.add(new ArrayList<Integer>());
  69. }
  70. else
  71. {
  72. argindex = map.get(next);
  73. }
  74.  
  75. adj.get(argindex).add(varindex);
  76.  
  77. }
  78.  
  79. }
  80.  
  81. visited = new boolean[adj.size()];
  82. recstack = new boolean[adj.size()];
  83. boolean cycle = false;
  84.  
  85.  
  86. if(cycle(0))
  87. {
  88. cycle = true;
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95. if(cycle || count != visited.length)
  96. System.out.println("Case #"+o+": BAD");
  97. else
  98. System.out.println("Case #"+o+": GOOD");
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. }
  112. static boolean cycle(int i)
  113. {
  114. if(visited[i] == false)
  115. { count++;
  116. visited[i] = true;
  117. recstack[i] = true;
  118.  
  119. int s = adj.get(i).size();
  120. ArrayList<Integer> aa = adj.get(i);
  121. for(int ii=0; ii<s;ii++)
  122. {
  123. int next = aa.get(ii);
  124.  
  125. if(!visited[next] && cycle(next))
  126. return true;
  127. else if(recstack[next])
  128. return true;
  129. }
  130.  
  131. }
  132. recstack[i] = false;
  133. return false;
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. }
  142.  
  143. static class InputReader {
  144. public BufferedReader reader;
  145. public StringTokenizer tokenizer;
  146.  
  147. public InputReader(InputStream stream) {
  148. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  149. tokenizer = null;
  150. }
  151.  
  152. public String next() {
  153. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  154. try {
  155. tokenizer = new StringTokenizer(reader.readLine());
  156. } catch (IOException e) {
  157. throw new RuntimeException(e);
  158. }
  159. }
  160. return tokenizer.nextToken();
  161. }
  162.  
  163. public int nextInt() {
  164. return Integer.parseInt(next());
  165. }
  166.  
  167. }
  168. }
Runtime error #stdin #stdout #stderr 0.04s 711168KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NumberFormatException: null
	at java.lang.Integer.parseInt(Integer.java:542)
	at java.lang.Integer.parseInt(Integer.java:615)
	at Main.main(Main.java:21)