Go: // Find the minimum spanning tree by the Kruskal algorithm
// -------------------------------------------------------
// Kruskal(G):
//     A = ∅
//     foreach v ∈ G.V:
//        MAKE-SET(v)
//     foreach (u, v) in G.E ordered by weight(u, v), increasing:
//         if FIND-SET(u) ≠ FIND-SET(v):
//             A = A ∪ {(u, v)}
//             UNION(u, v)
//     return A
//
// Ref: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm

package main

import (
	"bufio"
	"os"
	"sort"
	"strconv"
	"strings"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)
var (
	matrix [][]int
)

type Edge struct {
	a, b, cost, added int
}
type ByCost []Edge

var edges ByCost

func (a ByCost) Len() int           { return len(a) }
func (a ByCost) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByCost) Less(i, j int) bool { return a[i].cost < a[j].cost }

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		matrix = make([][]int, n)
		for i := range matrix {
			matrix[i] = make([]int, n)
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			cost, _ := strconv.Atoi(buf_[2])
			matrix[a][b] = cost
			matrix[b][a] = cost
		}
		sum := Kruskal(n)
		fmt.Println(sum)
		if len(edges) > 0 {
			edges = []Edge{}
		}
	}
}

func Kruskal_func(size, edge_num int) int {
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				tmp := Edge{i, j, matrix[i][j], 0}
				edges = append(edges, tmp)
			}
		}
	}
	sort.Sort(ByCost(edges))
	UF_Init(edge_num)
	for i := 0; i < edge_num; i++ {
		a := edges[i].a
		b := edges[i].b
		if UF_Find(a) != UF_Find(b) {
			edges[i].added = 1
			UF_Union(UF_Find(a), UF_Find(b))
		}
	}
	sum := 0
	for i := 0; i < edge_num; i++ {
		if edges[i].added != 0 {
			// log.Printf("edge(%d,%d) is added\n", edges[i].a, edges[i].b)
			sum += edges[i].cost
		}
	}
	return sum
}

func Kruskal(size int) int {
	edge_num := 0
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				edge_num++
			}
		}
	}
	return Kruskal_func(size, edge_num)
}

/*** Union-Find functions ***/
var UF_Parent []int
var UF_Rank []int

func UF_Init(n int) {
	UF_Parent = make([]int, n)
	UF_Rank = make([]int, n)
	for i := 0; i < n; i++ {
		UF_Parent[i] = i
		UF_Rank[i] = 0
	}
}
func UF_Union(a, b int) {
	if UF_Rank[a] < UF_Rank[b] {
		UF_Parent[a] = b
	} else {
		UF_Parent[b] = a
	}
	if UF_Rank[a] == UF_Rank[b] {
		UF_Rank[a]++
	}
}
func UF_Find(a int) int {
	if UF_Parent[a] == a {
		return a
	}
	return UF_Find(UF_Parent[a])
}
/*** Union-Find functions ***/

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s,d[101],f[101],g[101],h,i,j,size[101],ban[101];
	int z,x,q;
	scanf("%d %d",&a,&s);
	for(i=0;i<a;i++){
		ban[i]=i;
		size[i]=1;
	}
	for(i=0;i<s;i++) scanf("%d %d %d",&d[i],&f[i],&g[i]);
	for(i=0;i<s;i++){
		if(d[i]>f[i]) h=d[i]; d[i]=f[i]; f[i]=h;
		for(j=i+1;j<s;j++){
			if(g[i]>g[j]){
				h=g[i]; g[i]=g[j]; g[j]=h;
				h=d[i]; d[i]=d[j]; d[j]=h;
				h=f[i]; f[i]=f[j]; f[j]=h;
			}
		}
	}
	for(i=0;i<s;i++){
	//	printf("%d %d %d\n\n",d[i],f[i],g[i]);
		z=d[i]; x=f[i];
		while(ban[z]!=z) z=ban[z];
		while(ban[x]!=x) x=ban[x];
		if(z!=x){
			q+=g[i];
			if(size[z]>size[x]) ban[z]=x;
			else if(size[z]<size[x]) ban[x]=z;
			else{
				ban[x]=z;
				size[x]++;
			}
		}
	}
	printf("%d\n",q);
	return 0;
}

C++: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#define ll long long
#define ALL(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define vi vector<int>
#define vvi vector<vector<int>>
using namespace std;

class UnionFind {
private:
    vector<vector<int>> Graph; // {ID,Number,ParentID,Height}

    void flatten(int x, int rootid) {
        int parentid = Graph[x][0];
        while (Graph[parentid][0] != Graph[parentid][2]) {
            Graph[parentid][2] = rootid;
            int next = Graph[parentid][2];
            parentid = next;
        }
    }
public:
    void push(int x) {
        int i = Graph.size();
        Graph.push_back({ i,x,i,1 });
    }
    void unite(int x, int y) {
        int hx = Graph[root(x)][3];
        int hy = Graph[root(y)][3];
        if (hx < hy) {
            Graph[root(x)][2] = root(y);
            flatten(x, root(y));
        }
        else if(hx == hy){
            Graph[root(y)][2] = root(x);
            Graph[root(x)][3]++;
            flatten(y, root(x));
        }
        else {
            Graph[root(y)][2] = root(x);
            flatten(y, root(x));
        }
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }

   
    int root(int x) {
        int parentid = Graph[x][0];
        while (Graph[parentid][0] != Graph[parentid][2]) {
            int next = Graph[parentid][2];
            parentid = next;
        }
       
        return parentid;
    }
};

int sub(int n, int m) {

    vector<vector<int>> edges; // {weight, from_id, to_id} both 0-indexed

    for (int i = 0; i < m; i++) {
        int a, b, cost; cin >> a >> b >> cost;
        edges.push_back({ cost,a,b });
    }

    sort(ALL(edges));
    int n_edges = 0;
    int TotalCost = 0;

    UnionFind uf;
    for (int i = 0; i < n; i++) uf.push(i);

    for (int i = 0; n_edges < n - 1; i++) {
        vector<int> edge = edges[i];
       
        if (uf.root(edge[1]) != uf.root(edge[2])) {
            uf.unite(edge[1], edge[2]);
            n_edges++;
            TotalCost += edge[0];
        }
    }


    return TotalCost;
}

int main() {
    int n, m;
    vector<int> ans;
    while (true) {
        cin >> n >> m;

        if (n == 0 && m == 0) {
            break;
        }
        else {
            ans.push_back(sub(n, m));
        }
    }

    for (int elem : ans) {
        cout << elem << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Bridge
{
	int cost;
	int edge[ 2 ];
};

class BridgeAsc
{
public:
	bool operator()( const Bridge& a, const Bridge& b ) const
	{
		if( a.cost < b.cost ) return true;
		return false;
	};
};

int main()
{
	int n, m;
	while( cin >> n >> m && ( n || m ) ){
		vector< Bridge > bridge;
		for( int i = 0; i < m; ++i ){
			Bridge b;
			cin >> b.edge[ 0 ] >> b.edge[ 1 ] >> b.cost;
			bridge.push_back( b );
		}
		sort( bridge.begin(), bridge.end(), BridgeAsc() );

		int city[ 100 ] = {0};
		int id = 1;
		int cntBridge = 0;
		int index = 0;
		int ans = 0;
		while( cntBridge < n - 1 ){
			Bridge b = bridge[ index ];
			int con = 0;
			int x = b.edge[ 0 ];
			int y = b.edge[ 1 ];
			if( city[ x ] != 0 && city[ x ] == city[ y ] ){
				++index;
				continue;
			}
			if( city[ x ] == 0 && city[ y ] == 0 ){
				city[ x ] = city[ y ] = id++;
			}else if( city[ x ] == 0 ){
				city[ x ] = city[ y ];
			}else if( city[ y ] == 0 ){
				city[ y ] = city[ x ];
			}else{
				for( int i = 0; i < n; ++i ){
					if( city[ i ] == city[ y ] ){
						city[ i ] = city[ x ];
					}
				}
			}
			ans += b.cost;
			++cntBridge;
			++index;
		}
		cout << ans << endl;
	}

	return 0;
}