fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14.  
  15. public ArrayList<Integer> bfs(ArrayList<ArrayList<Integer>> adj) {
  16. int v = adj.size();
  17. boolean []visited = new boolean[v];
  18. ArrayList<Integer> res = new ArrayList<>();
  19. int src = 0;
  20. Queue<Integer> q = new LinkedList<>();
  21. q.add(src);
  22. visited[src] = true;
  23. while(!q.isEmpty()){
  24. int curr = q.poll();
  25. res.add(curr);
  26.  
  27. for(int x : adj.get(curr)){
  28.  
  29. if(!visited[x]){
  30. q.add(x);
  31. visited[x] = true;
  32. }
  33. }
  34.  
  35. }
  36.  
  37. return res;
  38.  
  39. }
  40. }
Success #stdin #stdout 0.08s 54624KB
stdin
Standard input is empty
stdout
Standard output is empty