HackerRank-Journey to the Moon

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
    
    
    public static long journeyToMoon(int n, List<List<Integer>> astronaut) {
    // Write your code here
        long answer = 0;
    
        List<List<Integer>> graph = new ArrayList<>();
        for(int i = 0; i < n; i ++) {
            graph.add(new ArrayList());
        }
    
        for(List<Integer> pair : astronaut) {
            int n1 = pair.get(0);
            int n2 = pair.get(1);
            
            graph.get(n1).add(n2);
            graph.get(n2).add(n1);
        }
        
        boolean[] visited = new boolean[n];
        List<Integer> graphCounts = new ArrayList<>();
        
        for (int i = 0; i < n; i++) {
            if (visited[i] == false) {
                int count = bfs(visited, graph, i);
                graphCounts.add(count);
            }
        }
        long total = 0;
        for (int graphCount : graphCounts) {
            
            answer += graphCount * total;
            total += graphCount; 
        }
        
        return answer;
        
        
    }
    
    public static int bfs(boolean[] visited, List<List<Integer>> graph, int startNode) {
        
        int count = 0;
        Queue<Integer> queue = new LinkedList<>();
        queue.add(startNode);
        visited[startNode] = true;
        
        while(!queue.isEmpty()) {
            int currentNode = queue.poll();
     
            count += 1; 
            List<Integer> nextNodes = graph.get(currentNode);
            for (Integer nextNode: nextNodes) {
                if (!visited[nextNode]) {
                    queue.add(nextNode);
                    visited[nextNode] = true;
                }
            }
        }
        
        return count;
    }
}
public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
        int n = Integer.parseInt(firstMultipleInput[0]);
        int p = Integer.parseInt(firstMultipleInput[1]);
        List<List<Integer>> astronaut = new ArrayList<>();
        IntStream.range(0, p).forEach(i -> {
            try {
                astronaut.add(
                    Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                        .map(Integer::parseInt)
                        .collect(toList())
                );
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
        long result = Result.journeyToMoon(n, astronaut);
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
        bufferedReader.close();
        bufferedWriter.close();
    }
}
case 1 
5 3
0 1
2 3
0 4
6
case 2 
100000 2
1 2
3 4
4999949998