(UVA) Come and Go - Solution

I used Kosaraju's algorithm to solve this problem.


import java.io.*;
import java.util.*;

class Main  {
    public static HashMap<Integer, ArrayList<Integer>> adjListOut;
    public static HashMap<Integer, ArrayList<Integer>> adjListOutReverse;
    public static boolean[] visited;
    public static Deque<Integer> stack;
   
    public static void dfsReverse(int intersection) {
        if (visited[intersection]) {
            return;
        }
       
        visited[intersection] = true;
        ArrayList<Integer> reachIntersec = adjListOutReverse.get(intersection);
        for (int i = 0; i < reachIntersec.size(); i++) {
            dfsReverse(reachIntersec.get(i));
        }
    }
   
    public static void dfs(int intersection) {
        if (visited[intersection]) {
            return;
        }
       
        visited[intersection] = true;
        ArrayList<Integer> reachIntersec = adjListOut.get(intersection);
        for (int i = 0; i < reachIntersec.size(); i++) {
            dfs(reachIntersec.get(i));
        }
       
        stack.add(intersection);
    }
   
    public static void process() throws NumberFormatException, IOException {  
        Scanner sc = new Scanner(System.in);
       
        int numIntersections = sc.nextInt();
        int numStreets = sc.nextInt();
        while (numIntersections != 0 || numStreets != 0) {
            adjListOut = new HashMap<Integer, ArrayList<Integer>>();
            adjListOutReverse = new HashMap<Integer, ArrayList<Integer>>();
            for (int i = 1; i <= numIntersections; i++) {
                adjListOut.put(i, new ArrayList<Integer>());
                adjListOutReverse.put(i, new ArrayList<Integer>());
            }
           
            for (int i = 0; i < numStreets; i++) {
                int intersec1 = sc.nextInt();
                int intersec2 = sc.nextInt();
                int oneTwoWay = sc.nextInt();
                adjListOut.get(intersec1).add(intersec2);
                adjListOutReverse.get(intersec2).add(intersec1);
                if (oneTwoWay == 2) {
                    adjListOut.get(intersec2).add(intersec1);
                    adjListOutReverse.get(intersec1).add(intersec2);
                }
            }
           
            stack = new ArrayDeque<Integer>();
            visited = new boolean[numIntersections+1];
            for (int i = 1; i <= numIntersections; i++) {
                dfs(i);
            }
           
            int stackSize = stack.size();
            int countConnectedComponents = 0;
            visited = new boolean[numIntersections+1];
            for (int i = 0; i < stackSize; i++) {
                int intersection = stack.pollLast();
                if (!visited[intersection]) {
                    dfsReverse(intersection);
                    countConnectedComponents++;
                }
            }
                           
            if (countConnectedComponents == 1) {
                System.out.println("1");
            }
            else {
                System.out.println("0");
            }
               
            numIntersections = sc.nextInt();
            numStreets = sc.nextInt();
        }
                                               
        return;
    }
  
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução