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 <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <time.h>
 
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FFOR(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,b) FOR(i,0,b)
#define RREP(i,b) FFOR(i,1,b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef int ut;
typedef long double ld;
typedef pair<ut,ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut,pr> ppr;
typedef priority_queue<pr,Vpr, greater<pr> > PQ;
const int SIZE=1e+5 + 1;
const ut INF=1<<30;
const ld eps=1e-6;
const LL mod=1e+9 + 7;

int N, M;
vector<ppr> G;

const int MAX_N = 1000;

int par[MAX_N + 1], r[MAX_N + 1];

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

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

int main() {
	while(cin >> N >> M && N){
		G.clear();
		REP(i,M){
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			G.PB(ppr(c, pr(a, b)));
		}
		init(N+1);
		sort(BE(G));
		int ans = 0;
		REP(i,G.size()){
			ppr e = G[i];
			if(!same(e.S.F, e.S.S)){
				unite(e.S.F, e.S.S);
				ans += e.F;
			}
		}
		cout << ans << endl;
	}
  return 0;
}

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

// ????????????????????£?¨?
int kruskal(int v, vector<vector<pair<int, int> > > adjlist);

// ?????????
int main(void){
  while(true){
    // ???????????°??¨???????????°???????????????
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }

    // ???????????±????????????????????£??\??????????????????
    vector<vector<pair<int, int> > > adjlist(n);
    for(int i = 0; i < m; i++){
      int a, b, d;
      scanf("%d %d %d", &a, &b, &d);
      adjlist[a].push_back(make_pair(d, b));
      adjlist[b].push_back(make_pair(d, a));
    }
  
    // ????°???¨?????¨????????????????±??????????????????????
    int length = kruskal(n, adjlist);
    cout << length << endl;
  }

  return 0;
}

// UnionFind??¨ ??????????????????

class UnionFindTree{
private:
  int m_size, unite_count;
  vector<int> parent;
  vector<int> rank;
  int find(int a);
public:
  void init(int size);
  void unite(int a, int b);
  bool same(int a, int b);
  bool isConnected(void);
};

// UF??¨????????????
void UnionFindTree::init(int size){
  m_size = size;
  unite_count = 0;
  parent.clear();
  rank.clear();
  for(int i = 0; i < size; i++){
    parent.push_back(i);
    rank.push_back(0);
  }
  return;
}

// UF??¨?????????????????????
int UnionFindTree::find(int a){
  if(parent[a] != a){ parent[a] = find(parent[a]); }
  return parent[a];
}

// UF??¨??§???????????£?????????
void UnionFindTree::unite(int a, int b){
  int x = find(a);
  int y = find(b);
  // ?????§???????????£???????????????????????????
  if(x == y){ return; }

  unite_count++;
  if(rank[x] < rank[y]){ parent[x] = y; }
  else{
    parent[y] = x;
    if(rank[x] == rank[y]){ rank[x]++; }
  }
  return;
}

// UF??¨??§???????????????????????????????????????
bool UnionFindTree::same(int a, int b){
  return find(a) == find(b);
}

// UF??¨??§?????????????????????????????¨???????????????????????????
bool UnionFindTree::isConnected(void){
  return unite_count == m_size - 1;
}

// ??????????????????
// v: ????????°, adjlist: ??£??\?????????(first: ???????????????, second: ?????????)
int kruskal(int v, vector<vector<pair<int, int> > > adjlist){
  int length = 0; // ??¨????????????

  // ?????????????????????
  vector<pair<int, pair<int, int> > > edge; // first: ???????????????, second: ???????????????
  for(int i = 0; i < v; i++)
    for(int j = 0; j < adjlist[i].size(); j++)
      edge.push_back(make_pair(adjlist[i][j].first, make_pair(i, adjlist[i][j].second)));
  sort(edge.begin(), edge.end());

  // UF??¨?????£?¨???¨?????????
  UnionFindTree uf;
  uf.init(v);

  // ???????????????????????????????????????????????????????????£?????????????????§?????????
  for(int i = 0; i < edge.size() && !uf.isConnected(); i++){
    // i??????????°???????????????????
    int cost = edge[i].first;
    int a = edge[i].second.first;
    int b = edge[i].second.second;
    // ????????????????????????????????£????????????????£???°???
    if(uf.same(a, b)){ continue; }

    // UF??¨?????£??????????????????????????´??°
    uf.unite(a, b);
    length += cost;
  }

  // ?????????: ??¨????????????
  return length;
}