Problem:
Minimum Spanning Tree

Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).

Input

|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1

, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.

Output

Print the sum of the weights of the Minimum Spanning Tree.

Constraints

1 ≤ |V| ≤ 10,000

0 ≤ |E| ≤ 100,000

0 ≤ wi ≤ 10,000

The graph is connected

There are no parallel edges

There are no self-loops

Sample Input 1

4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5

Sample Output 1

3

Sample Input 2

6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6

Sample Output 2

5

Solution in C# (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;

namespace AOJ
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Trim().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }


        //15.5最小全域木 kruskal
        static long kruskal(int N, List<Edge> edges)
        {
            int totalcost = 0;
            edges.Sort((l, r) => l.cost - r.cost);
            DisjointSet dest = new DisjointSet(N);
            for (var i = 0; i < edges.Count; ++i)
            {
                if (!dest.same(edges[i].source, edges[i].target))
                {
                    totalcost += edges[i].cost;
                    dest.unite(edges[i].source, edges[i].target);
                }
            }
            return totalcost;
        }

        static void Main(string[] args)
        {
            var VE = Sarray();
            var V = VE[0];
            var E = VE[1];

            var edges = new List<Edge>();
            for (var i = 0; i < E; ++i)
            {
                var stw = Sarray();
                edges.Add(new Edge((int)stw[0], (int)stw[1], (int)stw[2]));
            }
            WriteLine(kruskal((int)V,edges));
        }
        public struct Edge
        {
            public int source, target, cost;
            public Edge(int s,int t, int c)
            {
                source = s;
                target = t;
                cost = c;
            }
        }
        public class DisjointSet
        {
            int[] p, rank;
            public DisjointSet(int n)
            {
                p = new int[n];
                rank = new int[n];
                for (var i = 0; i < n; ++i)
                    makeSet(i);
            }
            public void makeSet(int x)
            {
                p[x] = x;
                rank[x] = 0;
            }
            public bool same(int x, int y)
            {
                return findSet(x) == findSet(y);
            }
            public void unite(int x, int y)
            {
                link(findSet(x), findSet(y));
            }
            void link(int x, int y)
            {
                if (rank[y] < rank[x])
                { p[y] = x; }
                else
                {
                    p[x] = y;
                    if (rank[x] == rank[y])
                        ++rank[y];
                }
            }
            int findSet(int x)
            {
                if (x != p[x])
                    p[x] = findSet(p[x]);
                return p[x];
            }
        }
    }
}