<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 UF {
public:
    UF(int n) {
        count = n;
        parent = vector<int>(n);
        for (int i = 0; i < n; i ++) {
            parent[i] = i;
        }
    }

    void union_(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        if (rootP == rootQ) return;
        parent[rootP] = rootQ;
        count -= 1;
    }

    int find(int x) {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    bool connect(int p, int q) {
        int rootP = find(p);
        int rootQ = find(q);
        return rootP == rootQ;
    }

    int getCount() {
        return count;
    }

private:
    int count;
    vector<int> parent;
};

bool compare (vector<int> edge1, vector<int> edge2) { return (edge1[2] < edge2[2]); }

class ComparisonClass {
public:
    bool operator() (const pair<int, pair<int, int>> l, const pair<int, pair<int, int>> r) const { 
        return l.first > r.first; 
    }
};


class Prim {
public:
    Prim (vector<vector<pair<int, pair<int, int>>>> g) {
        graph = g;
        int n = graph.size();  // number of nodes
        inMST = vector<bool>(n);
        inMST[0] = true;
        cut(0); // start from the first node
        while (pq.size() > 0) {
            pair<int, pair<int, int>> edge = pq.top(); pq.pop();
            int to = edge.second.second;
            int weight = edge.first;
            if (inMST[to]) {
                continue;
            }
            weightSum += weight;
            inMST[to] = true;
            cut(to);
        }
    }

    void cut(int s) {
        for (pair<int, pair<int, int>> edge: graph[s]) {
            int to = edge.second.second;
            if (inMST[to]) {
                continue;
            }
            pq.push(edge);
        }
    }

    int getWeightSum() {
        return weightSum;
    }

    bool allConnected() {
        for (int i = 0; i < inMST.size(); i ++) {
            if (!inMST[i]) {
                return false;
            }
        }
        return true;
    }

private:
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, ComparisonClass> pq;
    vector<vector<pair<int, pair<int, int>>>> graph;
    int weightSum = 0;
    vector<bool> inMST;
};


class Solution {
public:
    int minCostConnectPoints(vector<vector<int>>& points) {
        int n = points.size();
        vector<pair<int, pair<int, int>>> edges;
        vector<vector<pair<int, pair<int, int>>>> g(n);
        for (int i = 0; i < n; i ++) {
            for (int j = i + 1; j < n; j ++) {
                int x1 = points[i][0], y1 = points[i][1];
                int x2 = points[j][0], y2 = points[j][1];
                edges.push_back({abs(x1-x2)+abs(y1-y2), {i, j}});
                g[i].push_back({abs(x1-x2)+abs(y1-y2), {i, j}});
                g[j].push_back({abs(x1-x2)+abs(y1-y2), {j, i}});
            }
        }
        // return minimumCost(n, edges);
        return primMininumCost(g);
    }

    int primMininumCost(vector<vector<pair<int, pair<int, int>>>> g) {
        Prim prim(g);
        if (!prim.allConnected()) {
            return -1;
        }
        return prim.getWeightSum();
    }

    int minimumCost(int n, vector<pair<int, pair<int, int>>>& connections) {
        UF uf(n);
        int newEdges = 0;
        sort(connections.begin(), connections.end()); // use vector<pair<int, pair<int, int>>> can be faster in sort
        int mst = 0;
        for (int i = 0; i < connections.size() & newEdges < n - 1; i ++) {
            int weight = connections[i].first;
            int node1 = connections[i].second.first;
            int node2 = connections[i].second.second;
            if (uf.connect(node1, node2)) {
                continue;
            }
            mst += weight;
            uf.union_(node1, node2);
            newEdges += 1;
        }
        return uf.getCount() == 1 ? mst : 0;
    }
};

373635