Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main.java;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- List<List<Integer>> graph = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- String nextLine = sc.nextLine();
- if (nextLine.trim().equals("")) {
- graph.add(new ArrayList<>());
- } else {
- List<Integer> nextNodes = Arrays.stream(nextLine.split("\\s+"))
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- graph.add(nextNodes);
- }
- }
- List<Deque<Integer>> connectedComponents = getConnectedComponents(graph);
- for (Deque<Integer> connectedComponent : connectedComponents) {
- System.out.print("Connected component: ");
- for (int intNum : connectedComponent) {
- System.out.print(intNum + " ");
- }
- System.out.println();
- }
- }
- public static List<Deque<Integer>> getConnectedComponents(List<List<Integer>> graph) {
- // throw new AssertionError("Not Implemented");
- boolean[] visited = new boolean[graph.size()];
- List<Deque<Integer>> components = new ArrayList<>();
- for (int start = 0; start < graph.size(); start++) {
- if (!visited[start]) {
- components.add(new ArrayDeque<>());
- bfs(start, components, graph, visited);
- }
- }
- return components;
- }
- private static void bfs(int start, List<Deque<Integer>> components, List<List<Integer>> graph, boolean[] visited) {
- Deque<Integer> queue = new ArrayDeque<>();
- visited[start] = true;
- queue.offer(start);
- while (!queue.isEmpty()) {
- int node = queue.poll();
- components.get(components.size() - 1).offer(node);
- for (int child : graph.get(node)) {
- if (!visited[child]) {
- visited[child] = true;
- queue.offer(child);
- }
- }
- }
- }
- private static void dfs(int node, List<Deque<Integer>> components, List<List<Integer>> graph, boolean[] visited) {
- if (!visited[node]) {
- visited[node] = true;
- for (int child : graph.get(node)) {
- dfs(child, components, graph, visited);
- }
- components.get(components.size() - 1).offer(node);
- }
- }
- public static Collection<String> topSort(Map<String, List<String>> graph) {
- throw new AssertionError("Not Implemented");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment