2
public long countPairs(int n, int[][] edges) {4
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();5
for (int i = 0; i < n; i++) graph.add(new ArrayList<Integer>());6
for (int arr[] : edges) {7
graph.get(arr[0]).add(arr[1]);8
graph.get(arr[1]).add(arr[0]);10
boolean visited[] = new boolean[n];16
i++) { // Running for loop on all connected components of graph17
if (visited[i] == true)18
continue; // if the node is alredy reached by any of other vertex then we don't need to20
dfs(graph, i, visited, count);21
long a = n - count[0]; // (total - current count)22
long b = count[0] - prev; // (current count - prev )23
prev = count[0]; // Now Store count to prev29
void dfs(ArrayList<ArrayList<Integer>> graph, int v, boolean vis[], int count[]) {31
count[0]++; // for counting connected nodes32
for (int child : graph.get(v)) {34
dfs(graph, child, vis, count);