Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String solve2(InputStream in){
- // Read first line of input
- int nV = 0;
- int mE = 0;
- int startV = 0;
- int endV = 0;
- Scanner sc = new Scanner(in);
- if(sc.hasNextLine()){
- nV = sc.nextInt();
- mE = sc.nextInt();
- startV = sc.nextInt();
- endV = sc.nextInt();
- }
- // Create the Node List
- Node[] list = readData2(sc, nV);
- sc.close();
- if (mE == 0) return "no";
- if (nV == 0) return "no";
- if (startV==endV) return "yes";
- Node startNode = list[startV-1];
- Node endNode = list[endV-1];
- Queue<Node> queue = new LinkedList<>();
- queue.offer(startNode);
- while(queue.size()!=0){
- Node current = queue.poll();
- if(endNode == current) return "yes";
- current.marked = true;
- for(int i=0; i < current.outEdges.size();i++)
- if(current.outEdges.get(i).marked==false) queue.offer(current.outEdges.get(i));
- }
- return "no";
- }
- public static Node[] readData2(Scanner sc, int size){
- Node[] list = new Node[size];
- while(sc.hasNextLine()){
- //get the node
- int nodeAt = sc.nextInt() - 1;
- int edgeTo = sc.nextInt() - 1;
- if(list[edgeTo]==null) {
- list[edgeTo] = new Node();
- }
- if(list[nodeAt]==null) {
- list[nodeAt] = new Node();
- }
- list[nodeAt].outEdges.add(list[edgeTo]);
- // Ignore the rest
- sc.nextLine();
- }
- sc.close();
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement