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>
using namespace std;
#define MAX 101
#define INF (1<<21)

int n,bri[MAX][MAX];

void prim(){
  bool visit[MAX];
  int cost[MAX],d[MAX];

  for(int i = 0 ; i < n ; i++){
    visit[i] = false;
    cost[i] = INF;
  }

  cost[0] = 0;
  d[0] = -1;

  while(true){
    int u ,mincost = INF;
    for(int i = 0 ; i < n ; i++){
      if(!visit[i]&&cost[i] < mincost){
	mincost = cost[i];
	u = i;
      }
    }
    if(mincost == INF) break;
    visit[u] = true;

    for(int j = 0 ; j < n ; j++){
      if(visit[j] || bri[u][j] == INF) continue;
      if(bri[u][j] < cost[j]){
	cost[j] = bri[u][j];
	d[j] = u;
      }
    } 
  }

  int sum = 0;
  for(int i = 0 ; i < n ; i++){
    if(d[i] == -1) continue;
    sum += bri[i][d[i]];
  }

  cout << sum << endl;
}

int main(){
  int m,a,b,c;

  while(cin >> n >> m , n + m){
    for(int i = 0 ; i < MAX ; i++)
      for(int j = 0 ; j < MAX ; j++)
	bri[i][j] = INF;

    for(int i = 0 ; i < m ; i++){
	cin >> a >> b >> c;
	bri[a][b] = bri[b][a] = c;
    }

    prim();
  }

  return 0;
}

C++: //#include<stdio.h>
//#include<stdlib.h>
//int N,M,t[101],i,j,r;
//typedef struct e{int f,t,c;}e;
//e B[101];
//int cmp(const void *a,const void *b){e x=*(e*)a,y=*(e*)b;return x.c-y.c;}
//void in(n){for(;n--;)t[n]=n;}
//int p(n){return t[n]=t[n]==n?n:p(t[n]);}
//void u(a,b){t[p(a)]=p(b);}
//int s(a,b){return p(a)==p(b);}
//
//main()
//{
//	for(;scanf("%d%d",&N,&M),N,M;)
//	{
//		in(N);
//		for(i=r=0;i<M;i++)
//			scanf("%d%d%d",&B[i].f,&B[i].t,&B[i].c);
//		qsort(B,M,sizeof(e),cmp);
//		for(i=0;i<M;i++)
//			if(!s(B[i].f,B[i].t))
//			{
//				u(B[i].f,B[i].t);
//				r+=B[i].c;
//			}
//		printf("%d\n",r);
//	}
//	return 0;
//}
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cmath>
#define MAX_N 10005
using namespace std;

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

bool mcmp(edge x,edge y)
{
	return x.cost<y.cost;
}

int uft[MAX_N];
vector<edge>e;

void init(int n)
{
	int i;
	for (i=0;i<=n;i++)
	uft[i]=i;
}

int parent(int n)
{
	if (uft[n]==n)
		return n;
	else
		return uft[n]=parent(uft[n]);
}

void unit(int p, int q)
{
	uft[parent(p)]=parent(q);
}

int same(int p, int q)
{
	return parent(p)==parent(q);
}

int kruskal(int n,int r)
{
	int res=0;
	sort(e.begin(),e.end(),mcmp);
	init(n);
	for(int i=0;i<r;i++)
	{
		edge tmp=e[i];
		if(!same(tmp.from,tmp.to))
		{
			res+=tmp.cost;
			unit(tmp.from,tmp.to);
		}
	}
	return res;
}

int piles[10001][2];
int m_distance(int f,int t)
{
	return pow((double)piles[f][0]-piles[t][0],2)+pow((double)piles[f][1]-piles[t][1],2);
}

int main()
{
	int N,M;
	for(;scanf("%d%d",&N,&M),N,M;)
	{
		for(int j=0;j<M;j++)
		{
			edge tmp;
			scanf("%d%d%d",&tmp.from,&tmp.to,&tmp.cost);
			e.push_back(tmp);
		}
		printf("%d\n",kruskal(N,M));
		e.clear();
	}
	return 0;
}

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

using namespace std;

struct Edge {
	int a, b, cost;
};

int n, m;
int root[100], high[100];
Edge e[100000];

void init(int n) {
	for (int i = 0; i < n; i++)root[i] = i, high[i] = 1;
}

int getroot(int n){
	if (n == root[n])return n;
	return root[n] = getroot(root[n]);
}

bool issame(int a, int b) {
	return getroot(a) == getroot(b);
}

void unite(int a, int b) {
	a = getroot(a), b = getroot(b);
	if (a == b)return;
	if (high[a] > high[b])root[b] = a;
	else if (high[a] < high[b])root[a] = b;
	else if (high[a] == high[b])root[a] = b, high[a]++;
}

int main() {
	while (cin >> n >> m, n + m) {
		for (int i = 0; i < m; i++) {
			cin >> e[i].a >> e[i].b >> e[i].cost;
		}
		sort(e, e + m, [](Edge& l, Edge& r) {
			return l.cost < r.cost;
		});
		int ans = 0;
		init(n);
		for (int i = 0; i < m; i++) {
			if (!issame(e[i].a, e[i].b)) {
				unite(e[i].a, e[i].b);
				ans += e[i].cost;
			}
		}
		cout << ans << endl;
	}
	return 0;
}