(UVA) Vertex - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=216&mosmsg=Submission+received+with+ID+17300865

We have a directed graph, and the problem wants us to find all the inaccessible vertices. The Breadth-First Search is one option to solve this problem once it finds all the nodes that are connected to a starting node.


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

class Main {
    public static HashMap<Integer, ArrayList<Integer>> adjList;
    public static int[] visited;
   
    public static int bfs(int start) {
        Queue<Integer> queue = new ArrayDeque<>();
        queue.add(start);
       
        boolean first = true;
        int countVisited = 0;
        while (queue.size() > 0) {
            int currVertex = queue.poll();
           
            if (visited[currVertex] == 1) {
                continue;
            }
            if (!first) {
                visited[currVertex] = 1;
                countVisited++;
            }
            first = false;
                   
            ArrayList<Integer> reachVertex = adjList.get(currVertex);
            for (int i = 0; i < reachVertex.size(); i++) {
                queue.add(reachVertex.get(i));
            }
        }
       
        return countVisited;
    }
   
    public static void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        int numVertex = sc.nextInt();
        while (numVertex != 0) {
            adjList = new HashMap<>();
            for (int i = 1; i <= numVertex; i++) {
                adjList.put(i, new ArrayList<Integer>());
            }
           
            int vertex = sc.nextInt();
            while (vertex != 0) {
                int reachVertex = sc.nextInt();
                while (reachVertex != 0) {
                    adjList.get(vertex).add(reachVertex);
                    reachVertex = sc.nextInt();
                }
                vertex = sc.nextInt();
            }
           
            int numQueries = sc.nextInt();
            for (int i = 0; i < numQueries; i++) {
                visited = new int[numVertex+1];
                int start = sc.nextInt();
                int countAccessible = bfs(start);
                System.out.print(numVertex-countAccessible);
                for (int j = 1; j <= numVertex; j++) {
                    if (visited[j] == 0) {
                        System.out.print(" " + j);
                    }
                }
                System.out.println();
            }
           
            numVertex = 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