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<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;//都市の数
int m;//橋の数
/*クラスカル法*/
const int MAX_E = 110;
const int MAX_V = 110;
int V,E;//頂点数、辺の数
struct edge{
	int u,v,cost;//頂点uからvへのcost
};
edge es[MAX_E];
//union-find
int parent[MAX_V];//parent[x]=xの時xが根である
int rank[MAX_V];//木の高さ
void init(int V){
	for(int i= 0;i < V;i++){
		parent[i] = i;
		rank[i] =0;
	}
}

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

void unit(int x,int y){
	x = find(x);
	y = find(y);
	if(x == y)
		return;
	if(rank[x] < rank[y]){
		parent[x] = y;
	}
	else{
		parent[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){
	return find(x) == find(y);
}
bool comp(const edge& e1,const edge& e2){
	return e1.cost <e2.cost;
}

int kruskal(){
	sort(es,es+E,comp);
	init(V);
	int res = 0;
	for(int i = 0;i < E;i++){
		edge e = es[i];
		if(!same(e.u,e.v)){
			unit(e.u,e.v);
			res += e.cost;
		}
	}
	return res;
}


int main(){
	while(cin>>V>>E,V||E){
		rep(i,E){
			es[i].u=es[i].v,es[i].cost = 0;
		}
		rep(i,E){
			cin>>es[i].u>>es[i].v>>es[i].cost;
		}
		cout <<kruskal()<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N, M, A[10010], B[10010], COST[10010], parent[10010];
pair<int, int> bridge[10010];

void init(int N){
  for(int i=0; i<N; ++i) parent[i] = i;
}
int root(int a){
  if(parent[a] == a) return a;
  return (parent[a] = root(parent[a]));
}
bool is_same_set(int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
  parent[root(a)] = root(b);
}

int main(){
  while(cin >> N >> M && N > 0){
    init(N);
    for(int i=0; i<M; ++i){
      cin >> A[i] >> B[i] >> COST[i];
      bridge[i].first = COST[i];
      bridge[i].second = i;
    }
    sort(bridge, bridge+M);

    int sum = 0;

    for(int i=0; i<M; ++i){
      int cost = bridge[i].first;
      int a = A[bridge[i].second];
      int b = B[bridge[i].second];
      if(is_same_set(a, b)) continue;
      unite(a, b);
      sum += cost;
    }
    cout << sum << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define REP(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) REP(i,0,a)
using namespace std;

int par[101]; // parent
int ranks[101]; // depth of tree

void init(int n) {
    rep(i, n) {
        par[i] = i;
        ranks[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 (ranks[x] < ranks[y]) {
        par[x] = y;
    }
    else {
        par[y] = x;
        if (ranks[x] == ranks[y]) ranks[x]++;
    }
}

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

struct edge {int u; int v; int cost; };

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

// Entry point
int main(void){
    while (1) {
        int N,M;
        cin >> N >> M;
        if (N == 0) break;
        
        edge es[M];
        rep(i,M) cin >> es[i].u >> es[i].v >> es[i].cost;
        sort(es, es+M, comp);
        
        init(N);
        int ans = 0;
        rep(i,M) {
            edge e = es[i];
            if (!same(e.u, e.v)) {
                unite(e.u, e.v);
                ans += e.cost;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>  

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; -i)
#define INF (1 << 20)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, M;
int cost[101][101];
int mincost[101];
bool used[101];

int prim() {

	rep(i, N) {
		mincost[i] = INF;
		used[i] = false;
	}

	mincost[0] = 0;
	int res = 0;

	while(true) {
		int v = -1;
		rep(u, N) if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
		if(v == -1) break;
		used[v] = true;
		res += mincost[v];
		rep(u, N) mincost[u] = min(mincost[u], cost[v][u]);
	}
	return res;
}

int main() {
	while(scanf("%d %d", &N, &M), N || M) {
		rep(i, 101) rep(j, 101) cost[i][j] = INF;
		rep(i, M) {
			int a, b, Cost;
			scanf("%d %d %d", &a, &b, &Cost);
			cost[a][b] = cost[b][a] = Cost;
		}
		printf("%d\n", prim());
	}
	return 0;
}