<p>You are given an array <code>points</code> representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>

<p>The cost of connecting two points <code>[x<sub>i</sub>, y<sub>i</sub>]</code> and <code>[x<sub>j</sub>, y<sub>j</sub>]</code> is the <strong>manhattan distance</strong> between them: <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>, where <code>|val|</code> denotes the absolute value of <code>val</code>.</p>

<p>Return <em>the minimum cost to make all points connected.</em> All points are connected if there is <strong>exactly one</strong> simple path between any two points.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/26/d.png" style="width: 214px; height: 268px;" />
<pre>
<strong>Input:</strong> points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
<strong>Output:</strong> 20
<strong>Explanation:</strong> 
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/26/c.png" style="width: 214px; height: 268px;" />
We can connect the points as shown above to get the minimum cost of 20.
Notice that there is a unique path between every pair of points.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> points = [[3,12],[-2,5],[-4,1]]
<strong>Output:</strong> 18
</pre>

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

<ul>
	<li><code>1 &lt;= points.length &lt;= 1000</code></li>
	<li><code>-10<sup>6</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>6</sup></code></li>
	<li>All pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are distinct.</li>
</ul>

class Solution {
    struct Edge {
        int p1;
        int p2;
        int weight;
    };
    class DisjointSet {
        struct Node {
            int parent = -1;
            int rank = 1;
        };
        mutable vector<Node> nodes;

    public:
        DisjointSet(int size) : nodes(size) {
            for(int i = 0; i < size; i++)
                nodes[i].parent = i;
        }
        int find(int n) const {
            if(nodes[n].parent == n)
                return n;
            return nodes[n].parent = find(nodes[n].parent);
        }
        bool merge(int n1, int n2) {
            const int rn1 = find(n1);
            const int rn2 = find(n2);

            if(rn1 == rn2)
                return false;

            if(nodes[rn1].rank >= nodes[rn2].rank) {
                nodes[rn2].parent = rn1;
                if(nodes[rn1].rank == nodes[rn2].rank)
                    nodes[rn1].rank++;
            } else {
                nodes[rn1].parent = rn2;
            }
            return true;
        }
    };
public:
    int minCostConnectPoints(vector<vector<int>>& points) {
        const int pCount = points.size();
        if(pCount <= 1)
            return 0;

        list<Edge> edges;
        for(int p1 = 0; p1 < pCount - 1; p1++) {
            for(int p2 = p1 + 1; p2 < pCount; p2++) {
                const int dist = abs(points[p1][0] - points[p2][0]) + abs(points[p1][1] - points[p2][1]);
                edges.push_back({p1, p2, dist});
            }
        }

        DisjointSet djs(pCount);
        edges.sort([] (const Edge& e1, const Edge& e2) -> bool  { return e1.weight < e2.weight; });
        int cost = 0;
        int vCount = 1;
        for(const auto& e : edges) {
            if(djs.merge(e.p1, e.p2)) {
                cost += e.weight;
                vCount++;
            }
            if(vCount == pCount)
                break;
        }
        return cost;
    }
};
114975