<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> parent; // yeh parent vector hai, har node ka parent store karega
    vector<int> rank;   // yeh rank vector hai, tree ka height track karega (rank ka use hota hai union by rank mein)

    // Yeh find function hai, jo parent ko dhundta hai aur path compression bhi karta hai.
    // Time Complexity: O(α(n)) where α is inverse Ackermann function (bohot slow grow hoti hai, lagbhag constant time ke barabar)
    int find(int x) {
        if (x == parent[x]) return x; // agar apna hi parent hai toh return karo
        return parent[x] = find(parent[x]); // path compression (direct parent ko set karna)
    }

    // Union function do sets ko combine karega.
    // Time Complexity: O(α(n)) because of path compression and union by rank
    void Union(int x, int y) {
        int x1 = find(x); // x ka root dhundo
        int x2 = find(y); // y ka root dhundo
        if (x1 == x2) return; // agar dono ka parent same hai, toh kuch nahi karna

        // Union by rank: rank ka comparison karke tree ko balanced rakho
        if (rank[x1] > rank[x2]) {
            parent[x2] = x1; // x1 bada hai toh x2 ko x1 ka child banao
        } else if (rank[x1] < rank[x2]) {
            parent[x1] = x2; // x2 bada hai toh x1 ko x2 ka child banao
        } else {
            parent[x1] = x2; // rank barabar hai, kisi ko bhi parent banao, par rank increment karo
            rank[x2]++;
        }
    }

    // CountPairs function jo connected component size ka use karke valid pairs count karega
    // Time Complexity: O(n + m) where n is number of nodes and m is number of edges
    long long countPairs(int n, vector<vector<int>>& edges) {
        parent.resize(n); // parent array ko resize karo
        rank.resize(n, 0); // rank array ko initialize karo with 0

        // Har node ko apna khud ka parent banao initially
        // Time Complexity: O(n) for initialization
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        // Sabhi edges ko union karo (do nodes ko jodo)
        // Time Complexity: O(m * α(n)), m edges hain aur har union mein path compression lag raha hai
        for (auto& vec : edges) {
            int u = vec[0];
            int v = vec[1];
            Union(u, v);
        }

        // Har component ka size count karo
        // Time Complexity: O(n) because har node ke liye find kar rahe hain
        vector<int> componentSize(n, 0);
        for (int i = 0; i < n; i++) {
            int p = find(i); // find function use karke parent dhundo
            componentSize[p]++; // component size increment karo
        }

        // Valid pairs count karo
        long long res = 0;
        long long rem = n;
        // Time Complexity: O(n) for iterating over component sizes
        for (int i = 0; i < n; i++) {
            if (componentSize[i] > 0) { // sirf valid components ke liye
                long long size = componentSize[i];
                res += (size) * (rem - size); // formula for counting valid pairs
                rem -= size; // remaining nodes ko update karo
            }
        }

        return res; // total valid pairs return karo
    }
};

142140