<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 UnionFind {
// private:
//     vector<int> parent, rank;

// public:
//     UnionFind(int size) {
//         parent.resize(size);
//         rank.resize(size, 0);
//         for (int i = 0; i < size; i++) {
//             parent[i] = i;
//         }
//     }
//     int find(int x) {
//         if (parent[x] != x) parent[x] = find(parent[x]);
//         return parent[x];
//     }
//     void union_set(int x, int y) {
//         int xset = find(x), yset = find(y);
//         if (xset == yset) {
//             return;
//         } else if (rank[xset] < rank[yset]) {
//             parent[xset] = yset;
//         } else if (rank[xset] > rank[yset]) {
//             parent[yset] = xset;
//         } else {
//             parent[yset] = xset;
//             rank[xset]++;
//         }
//     }
// };

// class Solution {
// public:
//     long long countPairs(int n, vector<vector<int>>& edges) {
//         UnionFind dsu(n);
//         for (auto edge : edges) {
//             dsu.union_set(edge[0], edge[1]);
//         }
//         unordered_map<int, int> componentSize;
//         for (int i = 0; i < n; i++) {
//             componentSize[dsu.find(i)]++;
//         }

//         long long numberOfPaths = 0;
//         long long remainingNodes = n;
//         for (auto component : componentSize) {
//             int componentSize = component.second;
//             numberOfPaths += componentSize * (remainingNodes - componentSize);
//             remainingNodes -= componentSize;
//         }
//         return numberOfPaths;
//     }
// };
class UnionFind {
private:
    vector<int> sizes, parent;
public:
    UnionFind(int n) {
        sizes = vector<int>(n, 1);
        parent.resize(n);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
    }
    int find(int x) {
        if (parent[x] == x) {
            return x;
        }
        return parent[x] = find(parent[x]);
    }
    void connect(int x, int y) {
        int rootX = find(x), rootY = find(y);
        if (rootX != rootY) {
            if (sizes[rootX] > sizes[rootY]) {
                sizes[rootX] += sizes[rootY];
                parent[rootY] = rootX;
            } else {
                sizes[rootY] += sizes[rootX];
                parent[rootX] = rootY;
            }
        }
    }
    int getSize(int x) {
        return sizes[find(x)];
    }
};
class Solution {
public:
    long long countPairs(int n, vector<vector<int>>& edges) {
        // 4 * 2 + 2 * 1 + 1 * 4 = 14
        if (edges.size() == 0) {
            return (long long)n * (n - 1) / 2;
        } else if (edges.size() == 1) {
            return (long long)(n - 1) * (n - 2) / 2 + 2 * (n - 2); // unpaired ones by themselves
        }
        vector<int> componentSizes;
        vector<bool> included(n, false);
        UnionFind dsu(n);
        for (vector<int>& edge : edges) {
            dsu.connect(edge[0], edge[1]);
        }
        for (int i = 0; i < n; i++) {
            int root = dsu.find(i);
            if (!included[root]) {
                componentSizes.push_back(dsu.getSize(root));
                included[root] = true;
            }
        }
        long long pairs = 0;
        long long remainingNodes = n;
        for (int i = 0; i < componentSizes.size() - 1; i++) {
           pairs += (long long) componentSizes[i] * (remainingNodes - componentSizes[i]);
           remainingNodes -= componentSizes[i];
        }
        // for (int i = 0; i < componentSizes.size() - 1; i++) {
        //     for (int j = i + 1; j < componentSizes.size(); j++) {
        //         pairs += (long long) componentSizes[i] * componentSizes[j];
        //     }
        // }
        return pairs;
    }
};
140044