fork(4) download
  1.  
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. class Main
  6. {
  7. static int colour[];
  8. //static boolean[] visited;
  9. static ArrayList <Integer> [] graph;
  10. public static void main(String args[])
  11. throws IOException
  12. {
  13. int T=Integer.parseInt(br.readLine());
  14. for(int t=1;t<=T;t++)
  15. {
  16. String in[]=br.readLine().split(" ");
  17. int n=Integer.parseInt(in[0]);
  18. int m=Integer.parseInt(in[1]);
  19. //declaring array of arraylist such that the ith index of
  20. // the array will have all its neighbours in a list
  21. graph=new ArrayList[n+2];
  22. colour=new int[n+2];
  23. //visited=new boolean[n+2];
  24. for(int i=0;i<n+2;i++)
  25. graph[i]=new ArrayList<Integer>();
  26. //now for m different interactions
  27. for(int i=0;i<m;i++)
  28. {
  29. in=br.readLine().split(" ");
  30. int a=Integer.parseInt(in[0]);
  31. int b=Integer.parseInt(in[1]);
  32. graph[a].add(b);
  33. graph[b].add(a);
  34.  
  35. }
  36. boolean check=bfs(1);
  37. //System.out.println(check);
  38. if(!check)
  39. System.out.println("Scenario #"+t+":\nSuspicious bugs found!");
  40. else
  41. System.out.println("Scenario #"+t+":\nNo suspicious bugs found!");
  42.  
  43. //for(int j=0;j<colour.length;j++)
  44. //System.out.println(j+" "+colour[j]);
  45. }
  46.  
  47. }
  48.  
  49. static boolean bfs(int rootnode)
  50. {
  51. Queue<Integer> queue = new LinkedList();
  52. queue.add(rootnode);
  53. //visited[rootnode]=true;
  54. colour[rootnode]=1;
  55. while(!queue.isEmpty())
  56. {
  57. int u=queue.remove();//removing rootnode at beginning
  58. int base_colour=colour[u];
  59. //for each v in adj list of u
  60. ArrayList<Integer> adj_list=graph[u];
  61. Iterator<Integer> it=adj_list.iterator();
  62. while(it.hasNext())
  63. {
  64. int node=it.next();//only if node is unvisited
  65.  
  66. if(colour[node]!=0 && colour[node]==base_colour)
  67. return false;//not bipartite
  68.  
  69. if(colour[node]==0)
  70. {
  71. colour[node]=base_colour*-1;
  72. queue.add(node);
  73. //visited[node]=true;
  74. }
  75.  
  76. }
  77.  
  78. }
  79. return true;
  80. }
  81. }
  82.  
  83.  
  84.  
Success #stdin #stdout 0.08s 381248KB
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!