SvilenVelikov

Untitled

Jun 16th, 2020
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package main.java;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         int n = Integer.parseInt(sc.nextLine());
  12.         List<List<Integer>> graph = new ArrayList<>();
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             String nextLine = sc.nextLine();
  16.             if (nextLine.trim().equals("")) {
  17.                 graph.add(new ArrayList<>());
  18.             } else {
  19.                 List<Integer> nextNodes = Arrays.stream(nextLine.split("\\s+"))
  20.                         .map(Integer::parseInt)
  21.                         .collect(Collectors.toList());
  22.  
  23.                 graph.add(nextNodes);
  24.             }
  25.         }
  26.  
  27.         List<Deque<Integer>> connectedComponents = getConnectedComponents(graph);
  28.         for (Deque<Integer> connectedComponent : connectedComponents) {
  29.             System.out.print("Connected component: ");
  30.             for (int intNum : connectedComponent) {
  31.                 System.out.print(intNum + " ");
  32.             }
  33.             System.out.println();
  34.         }
  35.     }
  36.  
  37.     public static List<Deque<Integer>> getConnectedComponents(List<List<Integer>> graph) {
  38. //        throw new AssertionError("Not Implemented");
  39.         boolean[] visited = new boolean[graph.size()];
  40.         List<Deque<Integer>> components = new ArrayList<>();
  41.  
  42.         for (int start = 0; start < graph.size(); start++) {
  43.             if (!visited[start]) {
  44.                 components.add(new ArrayDeque<>());
  45.  
  46.                 bfs(start, components, graph, visited);
  47.             }
  48.         }
  49.  
  50.         return components;
  51.     }
  52.  
  53.     private static void bfs(int start, List<Deque<Integer>> components, List<List<Integer>> graph, boolean[] visited) {
  54.         Deque<Integer> queue = new ArrayDeque<>();
  55.         visited[start] = true;
  56.         queue.offer(start);
  57.  
  58.         while (!queue.isEmpty()) {
  59.             int node = queue.poll();
  60.  
  61.             components.get(components.size() - 1).offer(node);
  62.  
  63.             for (int child : graph.get(node)) {
  64.                 if (!visited[child]) {
  65.                     visited[child] = true;
  66.                     queue.offer(child);
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     private static void dfs(int node, List<Deque<Integer>> components, List<List<Integer>> graph, boolean[] visited) {
  73.         if (!visited[node]) {
  74.             visited[node] = true;
  75.             for (int child : graph.get(node)) {
  76.                 dfs(child, components, graph, visited);
  77.             }
  78.             components.get(components.size() - 1).offer(node);
  79.         }
  80.     }
  81.  
  82.  
  83.     public static Collection<String> topSort(Map<String, List<String>> graph) {
  84.         throw new AssertionError("Not Implemented");
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment