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 <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
using ll=long long;
using namespace std;
//const int INF = 7+(1e+9);
ll GCD(ll x, ll y)
{
    if (y == 0)
    {
        return x;
    }
    else
    {
        return GCD(y, x % y);
    }
}
ll LCM(ll x, ll y)
{
    return x / GCD(x, y) * y;
}
struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
vector<vector<char>> vec(12,vector<char>(12));

int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};
int dfs(int y,int x){
    vec[y][x]='5';
    for(int i=0;i<4;i++){
        if(x+dx[i]<12&&x+dx[i]>=0&&y+dy[i]<12&&y+dy[i]>=0&&vec[dy[i]+y][dx[i]+x]=='1'){
            dfs(y+dy[i],x+dx[i]);
        }
    }
    return 0;
}
int main(){
    int n,m;
    
    while(cin>>n>>m){
    vector<vector<int>> vec(m,vector<int>(3));
    for(int i=0;i<m;i++){
        int a,b,w;
        cin>>a>>b>>w;
        vec[i][0]=w;
        vec[i][1]=a;
        vec[i][2]=b;
    }
    sort(vec.begin(),vec.end());
    UnionFind tree(n);
    int sum=0;
    for(int i=0;i<m;i++){
        if(!tree.same(vec[i][1],vec[i][2])){
            tree.unite(vec[i][2],vec[i][1]);
            sum += vec[i][0];
        }
    }
    if(sum==0)break;
    cout<<sum<<endl;
    }
}

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) const {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);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;

int main() {
    int N, M;
    while (cin >> N >> M, N || M) {
        vector<vector<P> > V(N);
        for (int i = 0; i < M; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            V[a].push_back(P(c, b));
            V[b].push_back(P(c, a));
        }

        priority_queue<P, vector<P>, greater<P> > que;
        vector<int> used(N);
        que.push(P(0, 0));

        int sum = 0;
        while (que.size()) {
            P p = que.top(); que.pop();
            int now = p.second;
            int cost = p.first;

            if (used[now]) continue;
            used[now] = true;
            sum += cost;

            for (auto i : V[now]) {
                que.push(i);
            }
        }

        cout << sum << endl;
    }

    return 0;
}

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

struct edge { int u, v, cost; };
bool comp(const edge& e1, const edge& e2){
  return e1.cost < e2.cost;
}
int par[100];
int rank[100];

void init_union_find(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);
}




int n,m;
edge es[10000];
int V,E;

int kruskal(){
  sort (es,es + m, comp);
  init_union_find(n);
  int res = 0;
  for(int i=0;i<m;i++){
    edge e = es[i];
    if(!same(e.u,e.v)){
      unite(e.u,e.v);
      res+=e.cost;
    }
  }
  return res;
}

int main(){
  while(cin>>n>>m){
    if(n==0&&m==0)break;
    for(int i=0;i<m;i++){
      cin>>es[i].u>>es[i].v>>es[i].cost;
    }
    cout<<kruskal()<<endl;
  }
  
  return 0;
}