<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>

struct Node 
{
    int row, col; 
    Node(int _row, int _col): row(_row), col(_col) {}
    vector<pair<Node*, int>> vAdjPairNodes;
};

struct DisjointTree
{
    int groupId;
    DisjointTree(int _groupId): groupId(_groupId){}
    vector<Node*> vNodes;
};

class Solution {
public:
    int computeCost(Node* node1, Node* node2)
    {
        return abs(node1->row - node2->row) + abs(node1->col - node2->col);
    }

    int minSpanTree(vector<Node*>& vNodes)
    {
        map<Node*, DisjointTree*> mTrees;
        // Create a Disjoint trees such that each Disjoin tree consists of one of them 
        for (int idx = 0; idx < vNodes.size(); idx++)
        {
            mTrees[vNodes[idx]] = new DisjointTree(idx);
            mTrees[vNodes[idx]]->vNodes.push_back(vNodes[idx]);
        }
        // Create priority queue of edges and add lambda function to compare them by value 
        using PqElType = pair <pair<Node*, Node*>, int>;
        auto cmp = [](PqElType& left, PqElType& right)
        {
            return left.second > right.second;
        };
        priority_queue<PqElType, vector<PqElType>, decltype(cmp)> pqEdges;
        for (auto& node: vNodes)
        {
            for (auto& [adjNode, weight]: node->vAdjPairNodes)
            pqEdges.push({{node, adjNode}, weight});
        }
        int connComp = 0;
        int minCost = 0;
        // While not tree connected pick and edge from pq and if both edges belong to different trees 
        while ((connComp < vNodes.size() - 1) && (!pqEdges.empty()))
        {
            auto [nodes, weight] = pqEdges.top();
            pqEdges.pop();
            auto [node1, node2] = nodes;
            if (mTrees[node1] == mTrees[node2])
            {
                continue;
            }
            DisjointTree* destroyTree;
            DisjointTree* extendTree;
            if (mTrees[node1]->vNodes.size() > mTrees[node2]->vNodes.size())
            {
                destroyTree = mTrees[node2];
                extendTree = mTrees[node1];
            }
            else 
            {
                destroyTree = mTrees[node1];
                extendTree = mTrees[node2];
            }
            for (auto& node: destroyTree->vNodes)
            {
                extendTree->vNodes.push_back(node);
                mTrees[node] = extendTree;
            }
            delete destroyTree;
            cout << "W" << weight << endl;
            minCost += weight;
            connComp ++;
        }
        // join the trees and increment the counter
        // return the minimal cost. 
        return minCost;
    }

    int minCostConnectPoints(vector<vector<int>>& points) {
        vector<Node*> vNodes(points.size(), nullptr);
        for (int idx = 0; idx < vNodes.size(); idx++)
        {
            vNodes[idx] = new Node(points[idx][0], points[idx][1]);
        }
        for (int idx1 = 0; idx1 < vNodes.size(); idx1++)
        {
            for (int idx2 = idx1 + 1; idx2 < vNodes.size(); idx2++)
            {
                auto& node1 = vNodes[idx1];
                auto& node2 = vNodes[idx2];
                node1->vAdjPairNodes.push_back({node2, 
                computeCost(node1, node2)} );
                node2->vAdjPairNodes.push_back({node1, 
                computeCost(node1, node2)} );
            }   
        }
        int minCost = minSpanTree(vNodes);
        return minCost;
    }
};
364055