Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. public static String solve2(InputStream in){
  2.  
  3. // Read first line of input
  4. int nV = 0;
  5. int mE = 0;
  6. int startV = 0;
  7. int endV = 0;
  8. Scanner sc = new Scanner(in);
  9.  
  10. if(sc.hasNextLine()){
  11. nV = sc.nextInt();
  12. mE = sc.nextInt();
  13. startV = sc.nextInt();
  14. endV = sc.nextInt();
  15. }
  16.  
  17. // Create the Node List
  18. Node[] list = readData2(sc, nV);
  19. sc.close();
  20. if (mE == 0) return "no";
  21. if (nV == 0) return "no";
  22. if (startV==endV) return "yes";
  23. Node startNode = list[startV-1];
  24. Node endNode = list[endV-1];
  25.  
  26. Queue<Node> queue = new LinkedList<>();
  27.  
  28. queue.offer(startNode);
  29.  
  30. while(queue.size()!=0){
  31. Node current = queue.poll();
  32. if(endNode == current) return "yes";
  33. current.marked = true;
  34.  
  35. for(int i=0; i < current.outEdges.size();i++)
  36. if(current.outEdges.get(i).marked==false) queue.offer(current.outEdges.get(i));
  37. }
  38.  
  39. return "no";
  40. }
  41.  
  42. public static Node[] readData2(Scanner sc, int size){
  43. Node[] list = new Node[size];
  44. while(sc.hasNextLine()){
  45. //get the node
  46. int nodeAt = sc.nextInt() - 1;
  47. int edgeTo = sc.nextInt() - 1;
  48.  
  49. if(list[edgeTo]==null) {
  50. list[edgeTo] = new Node();
  51. }
  52.  
  53. if(list[nodeAt]==null) {
  54. list[nodeAt] = new Node();
  55. }
  56. list[nodeAt].outEdges.add(list[edgeTo]);
  57. // Ignore the rest
  58. sc.nextLine();
  59. }
  60. sc.close();
  61. return list;
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement