<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>

<p>Return <em>the <strong>number of pairs</strong> of different nodes that are <strong>unreachable</strong> from each other</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-3.png" style="width: 267px; height: 169px;" />
<pre>
<strong>Input:</strong> n = 3, edges = [[0,1],[0,2],[1,2]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-2.png" style="width: 295px; height: 269px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
<strong>Output:</strong> 14
<strong>Explanation:</strong> There are 14 pairs of nodes that are unreachable from each other:
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
Therefore, we return 14.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= edges.length &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>There are no repeated edges.</li>
</ul>

// class Solution {
// public:

//     vector<int> ans;
//     int find(vector<int>& parent, int x){
//         return parent[x] = (parent[x] == -1)? -1: find(parent, parent[x]);
//     }

//     void DSU(vector<int>& parent, vector<int>& rank, int a, int b){
//         a = find(parent, a);
//         b = find(parent, b);

//         if(rank[a]>= rank[b]){
//             parent[b] = a;
//             rank[a]++;
//         }
//         else{
//             parent[a]= b;
//             rank[b]++;
//         }
//     }

//     // Now we need to calculate the connected component size and no of connected component

//     void connected_component(vector<int>& parent, int n){
//         long long component = 0;
//         for(int i = 0; i<n; i++){
//             int result = 0;
//             if(parent[i] == -1){
//                 for(int j= i; j<n; j++){
//                     if(parent[j] == i){
//                         result++;
//                     }
//                 }
//                 ans.push_back(result);
//             }
//         }
//     } 

//     long long result(int n, vector<int>& parent){
//         long long ans = 1;
//         for(int i =n-2; i>=0; i--){
//             parent[i] = parent[i]+ parent[i+1];
//             ans = parent[i]*parent[i+1];
//         }
//         return ans;
//     }
//     long long countPairs(int n, vector<vector<int>>& edges) {
//         vector<int>parent(n+1 ,-1);
//         vector<int>rank(n+1, 0);
//         int m = edges.size();
//         for(int i = 0; i<m; i++){
//             int x = edges[i][0];
//             int y = edges[i][1];
//             DSU(parent, rank, x, y);
//         }
//         connected_component(parent, n);
//         return result(n, parent);    
//     }
// };


class Solution {
public:
    vector<int> ans;

    int find(vector<int>& parent, int x) {
        if (parent[x] == x) {
            return x;
        }
        return parent[x] = find(parent, parent[x]); // Path compression
    }

    void DSU(vector<int>& parent, vector<int>& rank, int a, int b) {
        a = find(parent, a);
        b = find(parent, b);

        if (a != b) { // Only union if they are in different sets
            if (rank[a] >= rank[b]) {
                parent[b] = a;
                if (rank[a] == rank[b]) {
                    rank[a]++;
                }
            } else {
                parent[a] = b;
            }
        }
    }

    long long countPairs(int n, vector<vector<int>>& edges) {
        vector<int> parent(n), rank(n, 0), componentSize(n, 0);

        // Initialize each node to be its own parent (singleton set)
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        // Perform DSU (Union-Find) operations for each edge
        for (const auto& edge : edges) {
            int x = edge[0];
            int y = edge[1];
            DSU(parent, rank, x, y);
        }

        // Count the size of each connected component
        for (int i = 0; i < n; i++) {
            int root = find(parent, i);
            componentSize[root]++;
        }

        // Calculate the total number of unreachable pairs
        long long totalNodes = n;
        long long totalPairs = totalNodes * (totalNodes - 1) / 2;
        long long reachablePairs = 0;

        // For each component size, calculate the number of reachable pairs
        for (int i = 0; i < n; i++) {
            if (componentSize[i] > 0) {
                long long size = componentSize[i];
                reachablePairs += size * (size - 1) / 2;
            }
        }

        // The number of unreachable pairs is total pairs minus reachable pairs
        return totalPairs - reachablePairs;
    }
};

142140