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++: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)

int par[200];
int ran[200];

void init(int n){
	rep(i, n){
		par[i] = i;
		ran[i] = 0;
	}
}

int find(int x){
	if(x == par[x]){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

bool same(int x, int y){
	if(find(x) == find(y)) return true;
	else return false;
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	if(ran[x] > ran[y]){
		par[y] = x;
	}else{
		par[x] = y;
		if(ran[x] == ran[y]) ran[y]++;
	}
}
PP bridge[10000];

int main(){
	int n, m, a, b, c;
	int res;
	while(cin >> n >> m && (n ||m)){
		res = 0;
		init(n);
		rep(i, m){
			cin >> bridge[i].second.first >> bridge[i].second.second >> bridge[i].first;
		}
		sort(bridge, bridge + m);
		int pos = 0;
		while(n > 1){
			PP pp = bridge[pos++];
			a = pp.second.first;
			b = pp.second.second;
			c = pp.first;
			if(!same(a, b)){
				unite(a, b);
				n--;
				res += c;
			}
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#define  MAX 101


struct Dis
{
    int pa[MAX];
    int used[MAX];
    int n;
    Dis(int n):n(n)
    {
        for(int i=0;i<n;i++)
        {
            pa[i]=i;
            used[i]=0;
        }
    }
    void use(int i)
    {
        used[i]=1;
    }
    int root(int a)
    {
        if(pa[a]==a)
            return a;
        return pa[a]=root(pa[a]);
    }
    bool is_same_set(int a,int b)
    {
        return root(a)==root(b);
    }
    void unite(int n,int m)
    {
        use(n);
        use(m);
        pa[root(n)]=root(m);
    }
    bool is_connected()
    {
        int rt=-1;
        for(int i=0;i<n;i++)
        {
            if(rt>=0 &&used[i] && rt!=root(i))
                return false;
            if(used[i])
                rt=root(i);
        }
        return rt!=-1;
    }
};

int br[6000][3];

int getMin(int m)
{
    int tmp=-1;
    for(int i=0;i<m;i++)
    {
        if(br[i][2]!=-1)
        {
            if(tmp==-1 || br[tmp][2]>=br[i][2])
            {
                tmp=i;
            }
        }
    }
    if(tmp!=-1)
    {
        return tmp;
    }
}

int main(void)
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2 && n && m)
    {
        Dis dis(n);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",br[i],br[i]+1,br[i]+2);
        }
        int nc=0;
        int cost=0;
        while(nc<n)
        {
            int min=getMin();
            int a=br[min][0];
            int b=br[min][1];
            if(!dis.is_same_set(a,b))
            {
                dis.unite(a,b);
                nc++;
                cost+=br[min][2];
                br[min][2]=-1;
            }
        }
        printf("%d\n",cost);
    }
    return 0;
    
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int Par[100],Rank[100];
void Init(int n) {
	for(int i=0;i<n;i++) {
		Par[i]=i;
		Rank[i]=0;
	}
}
int Find(int x) {
	if(Par[x]==x) {
		return x;
	}else {
		return Par[x]=Find(Par[x]);
	}
}
void Unite(int x,int y) {
	x=Find(x);
	y=Find(y);
	if(x==y) return;
	if(Rank[x]<Rank[y]) {
		Par[x]=y;
	}else {
		Par[y]=x;
		if(Rank[x]==Rank[y])Rank[x]++;
	}
}
bool Same(int x,int y) {
	return Find(x)==Find(y);
}
struct Edge {
	int from,to,cost;
	Edge(int _f,int _t,int _c) {from=_f,to=_t,cost=_c;}
	bool operator < (const Edge& E) {return cost<E.cost;}
};
int main() {
	int n,m;
	while(scanf("%d %d",&n,&m),n) {
		vector<Edge> es;
		for(int i=0;i<m;i++) {
			int a,b,cost;
			scanf("%d %d %d",&a,&b,&cost);
			es.push_back(Edge(a,b,cost));
		}
		sort(es.begin(),es.end());
		Init(n);
		int sum=0;
		for(int i=0;i<es.size();i++) {
			if(!Same(es[i].from,es[i].to)) {
				Unite(es[i].from,es[i].to);
				sum+=es[i].cost;
			}
		}
		printf("%d\n",sum);
	}
}