# unreachble pair

```java
import java.util.*;

public class Solution {
    static long count; // Static variable to track component size

    public long countPairs(int n, int[][] edges) {
        // Step 1: Build adjacency list
        List<List<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
        for (int[] edge : edges) {
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        }

        // Step 2: Visited array
        boolean[] vis = new boolean[n];
        long answer = 0;

        // Step 3: DFS on each component
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                count = 0;  // Reset static counter for new component
                dfs(i, adj, vis);
                answer += count * (n - count); // Count unreachable pairs
            }
        }

        // Step 4: Each pair counted twice, divide by 2
        return answer / 2L;
    }

    // DFS using static counter
    private void dfs(int node, List<List<Integer>> adj, boolean[] vis) {
        vis[node] = true;
        count++;
        for (int neighbor : adj.get(node)) {
            if (!vis[neighbor]) {
                dfs(neighbor, adj, vis);
            }
        }
    }
}
```
