fork(3) download
  1.  
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6. class BUGLIFE
  7. {
  8. static int colour[];
  9. static boolean[] visited;
  10. static ArrayList <Integer> [] graph;
  11. public static void main(String args[])
  12. throws IOException
  13. {
  14. int T=Integer.parseInt(br.readLine());
  15. for(int t=1;t<=T;t++)
  16. {
  17. String in[]=br.readLine().split(" ");
  18. int n=Integer.parseInt(in[0]);
  19. int m=Integer.parseInt(in[1]);
  20. //declaring array of arraylist such that the ith index of
  21. // the array will have all its neighbours in a list
  22. graph=new ArrayList[n+2];
  23. colour=new int[n+2];
  24. visited=new boolean[n+2];
  25. Arrays.fill(visited, false);
  26.  
  27. for(int i=0;i<n+2;i++)
  28. graph[i]=new ArrayList<Integer>();
  29. //now for m different interactions
  30. for(int i=0;i<m;i++)
  31. {
  32. in=br.readLine().split(" ");
  33. int a=Integer.parseInt(in[0]);
  34. int b=Integer.parseInt(in[1]);
  35. graph[a].add(b);
  36. graph[b].add(a);
  37.  
  38. }
  39. boolean check=dfs(1);
  40.  
  41. if(!check)//
  42. System.out.println("Scenario #"+t+":\nSuspicious bugs found!");
  43. else
  44. System.out.println("Scenario #"+t+":\nNo suspicious bugs found!");
  45.  
  46.  
  47. }
  48.  
  49. }
  50.  
  51. static boolean dfs(int rootnode)
  52. {
  53. Stack<Integer> mystack =new Stack<Integer>();
  54. mystack.push(rootnode);
  55. colour[rootnode]=1;
  56. visited[rootnode]=true;
  57. while(!mystack.isEmpty())
  58. {
  59. int node=mystack.peek();
  60. int child=get_unvisited_uncoloured_childnode(node,colour[node]);
  61. if(child==Integer.MIN_VALUE)//may day may day
  62. {return false;}
  63. else if(child==0)
  64. mystack.pop();
  65. else
  66. mystack.push(child);
  67.  
  68. }
  69.  
  70. return true;
  71. }
  72.  
  73. static int get_unvisited_uncoloured_childnode(int node,int base_colour)
  74. {
  75. //for each V in the adjacent list of u
  76. ArrayList<Integer> adj_list=graph[node];
  77. Iterator<Integer> it=adj_list.iterator();
  78. while(it.hasNext())
  79. {
  80. int hold=it.next();
  81. if(visited[hold]==false)//if hold is not yet visited
  82. {
  83. colour[hold]=base_colour*-1;
  84. visited[hold]=true;
  85. return hold;
  86. }
  87. else if(visited[hold]==true && colour[hold]==base_colour)
  88. {
  89. //not bipartite
  90. return Integer.MIN_VALUE;
  91. }
  92.  
  93. }
  94.  
  95.  
  96. return 0;
  97. }
  98. }
  99.  
  100.  
Success #stdin #stdout 0.08s 380160KB
stdin
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
stdout
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!