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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;


struct Unionfind{
    vector<int> parents;
    int n;
    Unionfind(int n):n(n),parents(n,-1){ }

    int find(int x){
        if(parents[x] < 0) return x;
        else return parents[x] = find(parents[x]);
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);

        if(x==y) return;
        if(parents[x] > parents[y]) swap(x,y);
        parents[x] += parents[y];
        parents[y] = x;
    }  

   int size(int x){
       return -parents[find(x)];
   }

   bool same(int x,int y){
       return find(x) == find(y);
   }

   vector<int> members(int x){
       int root = find(x);
       vector<int> member;
       for(int i=0;i<n;i++){
           if(find(i)==root){
               member.push_back(i);
           }
       }
      return member;
  }

  int group_cnt(){
      int c = 0;
      rep(i,n){
          if(parents[i] < 0) ++c;
      }
      return c;
  }
};

struct edge{
    int from,to,cost;  
};

int main(){
    while(1){
        int n,m;
        cin >> n >> m;
        if(n==0 && m == 0) break;
        vector<edge> eg;
        rep(i,m){
            int a,b,c;
            cin >> a >> b >> c;
            eg.push_back(edge{a,b,c});
        }

        sort(eg.begin(),eg.end(),[](const edge &l,const edge &r){
            return l.cost < r.cost;
        });

        Unionfind uf(n);
        int ans = 0;

        rep(i,eg.size()){
            if(uf.same(eg[i].from,eg[i].to)) continue;
            uf.unite(eg[i].from,eg[i].to);
            ans += eg[i].cost;
        }
        
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int sx,sy;
int gx,gy;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vector<int>d(n,inf);
		vector<bool>used(n);
		vvi cost(n,vi(n));
		rep(i,n)rep(j,n)cost[i][j]=inf;
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			cost[a][b]=cost[b][a]=c;
		}
		d[0]=0;
		int out=0;
		while(1){
			int v=-1;
			rep(i,n)if(!used[i]&&(v==-1||d[v]>d[i]))v=i;
			if(v==-1)break;
			used[v]=true;
			out+=d[v];
			rep(i,n)d[i]=min(d[i],cost[v][i]);
		}
		cout<<out<<endl;
		//int sum=0;
		//rep(i,n)cout<<i<<" "<<d[i]<<endl;//sum+=d[i];
		//cout<<sum<<endl;
	}
}

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

struct E {
    int from, to, cost;
    E(int f, int t, int c) : from(f), to(t), cost(c) { }
    bool operator <(const E &x) const {
        return cost < x.cost;
    }
};

struct union_find {
    vector<int> par;
    union_find(int n) : par(n, -1) { }
    bool same(int x, int y) {
        x = root(x);
        y = root(y);
        return x == y;
    }

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x != y) {
            par[x] += par[y]; par[y] = x;
        }
    }

    int root(int x) {
        return par[x] < 0 ? x : par[x] = root(par[x]);
    }
};

int kruskal(vector<E> edges, const int N) {
    sort(edges.begin(), edges.end());
    union_find uf(N);
    int res = 0;
    for(int i = 0; i < (int)edges.size(); i++) {
        E e = edges[i];
        if(uf.same(e.from, e.to) == false) {
            res += e.cost;
            uf.merge(e.from, e.to);
        }
    }
    return res;
}

int main() {
    for(int N, M; cin >> N >> M, N + M != 0; ) {
        vector<E> edges;
        for(int i = 0; i < M; i++) {
            int f, t, c; cin >> f >> t >> c;
            edges.push_back(E(f, t, c));
        }
        int res = kruskal(edges, N);
        cout << res << endl;
    }
}