Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.29 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.TimeUnit;
  3.  
  4. public class Coba2{
  5.     public static void main(String[] args){
  6.         Scanner in = new Scanner(System.in);
  7.         int nodes, bridges, start;
  8.  
  9.         nodes = in.nextInt();
  10.         bridges = in.nextInt();
  11.         start = in.nextInt();
  12.  
  13.         //Node array initialization
  14.         int[] nodesArr = new int[nodes+1];
  15.         initArray(nodesArr);        
  16.  
  17.         //Matrix initialization      
  18.         LinkedList<LinkedList<Integer>> graph =  new LinkedList<>();
  19.         initList(graph, nodes);
  20.  
  21.         //Set the bridges in the matrix
  22.         setList(graph, bridges);
  23.  
  24.         //Timer Start
  25.         long startTime = System.nanoTime();
  26.         //trace the graph
  27.         System.out.println("Routes possible: ");
  28.         trace(graph, nodesArr, start, "");
  29.         //Timer End
  30.         long endTime = System.nanoTime();
  31.  
  32.  
  33.         //print Nodes where the graph is stuck
  34.         printEnds(nodesArr);  
  35.         //Print Time elapsed
  36.         long durationInMillis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
  37.         System.out.println("Time elapsed (in milliseconds): \n" + durationInMillis);
  38.  
  39.         in.close();
  40.     }
  41.  
  42.     public static void initList(LinkedList<LinkedList<Integer>> graph, int nodes){
  43.         for(int i = 0; i <= nodes; i++){
  44.             graph.add(new LinkedList<Integer>());
  45.             graph.get(i).add(i);
  46.         }
  47.     }
  48.  
  49.     public static void initArray(int[] arr){
  50.         for(int i = 1; i < arr.length; i++){
  51.             arr[i] = 0;
  52.         }
  53.     }
  54.  
  55.     public static void setList(LinkedList<LinkedList<Integer>> graph, int bridges){
  56.         int u, v;
  57.         Scanner in = new Scanner(System.in);
  58.  
  59.         for(int i = 0; i < bridges; i++){
  60.             u = in.nextInt();
  61.             v = in.nextInt();
  62.  
  63.             graph.get(u).add(v);
  64.         }
  65.  
  66.         in.close();
  67.         System.out.println();
  68.     }
  69.  
  70.     public static void trace(LinkedList<LinkedList<Integer>> graph, int[] nodesArr, int start, String route){
  71.        
  72.         int nstart, prevstate;
  73.         boolean advance = false;
  74.         route = route + Integer.toString(start) + '-';
  75.         if(nodesArr[start] == 0) nodesArr[start] = 1;
  76.         if(nodesArr[start] == 2) nodesArr[start] = 3;
  77.  
  78.         //find all children of the node
  79.         for(int i = 1; i < graph.get(start).size(); i++){
  80.            
  81.             //if a child is found, trace it deeper (DFS)
  82.             nstart = graph.get(start).get(i);
  83.             if(nodesArr[nstart] % 2 == 0){
  84.                 trace(graph, nodesArr, nstart, route);
  85.                 if (nodesArr[nstart] == 1) nodesArr[nstart] = 0;
  86.                 if (nodesArr[nstart] == 3) nodesArr[nstart] = 2;
  87.                 advance = true;
  88.             }
  89.         }
  90.  
  91.         //If already reached the end of route
  92.         if (!advance) {
  93.             System.out.println(route + "End of Route\n");
  94.             nodesArr[start] = 2;
  95.         }
  96.     }
  97.  
  98.     public static void printEnds(int[] arr){
  99.         System.out.println("Stucked in these islands: ");
  100.  
  101.         for(int i = 0; i < arr.length; i++){
  102.             if(arr[i] >= 2){
  103.                 System.out.printf("%d ", i);
  104.             }
  105.             /* System.out.printf("%d ", arr[i]); */
  106.         }
  107.         System.out.println();
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement