Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Minimum Spanning Tree

For a given weighted graph $G = (V, E)$, find the minimum spanning tree (MST) of $G$ and print total weight of edges belong to the MST.

Input

In the first line, an integer $n$ denoting the number of vertices in $G$ is given. In the following $n$ lines, a $n \times n$ adjacency matrix $A$ which represents $G$ is given.
$a_{ij}$ denotes the weight of edge connecting vertex $i$ and vertex $j$. If there is no edge between $i$ and $j$, $a_{ij}$ is given by -1.

Output

Print the total weight of the minimum spanning tree of $G$.

Constraints

$1 \leq n \leq 100$

$0 \leq a_{ij} \leq 2,000$ (if $a_{ij} \neq -1$)

$a_{ij} = a_{ji}$

$G$ is a connected graph

Sample Input 1

5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1

Sample Output 1

5

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    struct Edge
    {
        internal int u, v, cost;
    }

    class Union_Find
    {
        internal List<int> parents, rank;

        public Union_Find(int n)
        {
            parents = new List<int>();
            rank = new List<int>();

            for (int i = 0; i < n; i++)
            {
                parents.Add(i);
                rank.Add(0);
            }
        }

        public int Find(int x)
        {
            if (parents[x] == x) return x;
            else return parents[x] = Find(parents[x]);
        }

        public void Unite(int x, int y)
        {
            x = Find(x);
            y = Find(y);

            if (x == y) return;

            if (rank[x] < rank[y])
            {
                parents[x] = y;
            }
            else
            {
                parents[y] = x;
                if (rank[x] == rank[y]) rank[x]++;
            }
        }

        public bool Same(int x, int y)
        {
            return Find(x) == Find(y);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            List<Edge> es = new List<Edge>();

            for (int i = 0; i < n; i++)
            {
                int[] input = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), int.Parse);

                for (int j = 0; j < n; j++)
                {
                    if (input[j] != -1)
                    {
                        Edge temp;
                        temp.u = i;
                        temp.v = j;
                        temp.cost = input[j];
                        es.Add(temp);
                    }
                }
            }
            Console.WriteLine(Kruskal(es, n));
        }

        static bool Comp(Edge e1, Edge e2)
        {
            return e1.cost < e2.cost;
        }

        static int Kruskal(List<Edge> es, int n)
        {
            es.Sort((a, b) => a.cost - b.cost);
            Union_Find uf = new Union_Find(n);

            int res = 0;

            for (int i = 0; i < es.Count; i++)
            {
                Edge e = es[i];

                if (!uf.Same(e.u, e.v))
                {
                    uf.Unite(e.u, e.v);
                    res += e.cost;
                }
            }
            return res;
        }
    }
}