Journey to the Moon

  • + 0 comments

    Java O(n+p)

     class Result {
            public static long journeyToMoon(int n, List<List<Integer>> astronaut) {
                List<List<Integer>> adjList = new ArrayList<>();
                for (int i = 0; i < n; i++) {
                    adjList.add(new ArrayList<>());
                }
    
                for (List<Integer> pair : astronaut) {
                    int u = pair.get(0);
                    int v = pair.get(1);
                    adjList.get(u).add(v);
                    adjList.get(v).add(u);
                }
    
                boolean[] visited = new boolean[n];
                List<Integer> countrySizes = new ArrayList<>();
    
                for (int i = 0; i < n; i++) {
                    if (!visited[i]) {
                        int countrySize = dfs(i, adjList, visited);
                        countrySizes.add(countrySize);
                    }
                }
    
                long totalPairs = (long) n * (n - 1) / 2;
                long sameCountryPairs = 0;
                for (int size : countrySizes) {
                    sameCountryPairs += (long) size * (size - 1) / 2;
                }
    
                return totalPairs - sameCountryPairs;
            }
    
            private static int dfs(int node, List<List<Integer>> adjList, boolean[] visited) {
                Stack<Integer> stack = new Stack<>();
                stack.push(node);
                visited[node] = true;
                int size = 0;
    
                while (!stack.isEmpty()) {
                    int current = stack.pop();
                    size++;
                    for (int neighbor : adjList.get(current)) {
                        if (!visited[neighbor]) {
                            visited[neighbor] = true;
                            stack.push(neighbor);
                        }
                    }
                }
    
                return size;
            }
    
    }
    
    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); // Change int to long
    
                bufferedWriter.write(String.valueOf(result));
                bufferedWriter.newLine();
    
                bufferedReader.close();
                bufferedWriter.close();
            }
        }