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 <map>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

int data[101][101];

int prim( int n )
{
	vector<int> key( n, 99999999 );
	key[0] = 0;
	vector<int> pq;
	vector<bool> inQueue( n, true );
	for( int i = 0;i < n;i++ ){
		pq.push_back( key[i] );
	}

	while( !pq.empty() ){
		vector<int>::iterator it = min_element( pq.begin(), pq.end() );
		int u = distance( pq.begin(), it );
		pq.erase( it );
		inQueue[u] = false;

		for( int i = 0;i < n;i++ ){
			if( data[u][i] ){
				if( inQueue[i] ){
					int w = data[u][i];
					if( w < key[i] ){
						key[i] = w;
						pq[i] = w;
					}
				}
			}
		}
	}

	int answer = 0;
	for( int i = 0;i < n;i++ ){
		answer += key[i];
	}

	return answer;
}

int main(int argc, char const* argv[])
{
	int n,m;
	int a,b,cost;

	while( cin >> n >> m && n != 0 ){
		for( int i = 0;i < 101;i++ ){
			for( int j = 0;j < 101;j++ ){
				data[i][j] = 0;
			}
		}
		for( int i = 0;i < m;i++ ){
			cin >> a >> b >> cost;
			data[a][b] = cost;
			data[b][a] = cost;
		}

		int answer = prim( n );
		cout << answer << endl;
	}

	return 0;
}

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

using namespace std;

#define MAX_E 10000
#define MAX_V 100

int V,E;
int par[MAX_V],rank[MAX_V];
void init(){
  for(int i = 0 ; i < V ; 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 u,v,cost;
};

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

edge es[MAX_E];

int kruskal(){
  init();
  sort(es , es + E,comp);

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

int main(){
  int a,b,c;
  while(scanf("%d %d" ,&V ,&E) ,(V | E)){
    for(int i = 0 ; i < E ; i++){
      scanf("%d %d %d" ,&a ,&b ,&c);
      es[i] = (edge){a,b,c};
    }
    printf("%d\n" ,kruskal());
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int p[110];
typedef pair<int,pair<int,int> >P;
priority_queue<P,vector<P>,greater<P> >Q;
int main(){
  int n,m;
  while(cin>>n>>m&&n+m){
    for(int i=0;i<m;i++){
      int a,b,c;
      cin>>a>>b>>c;
      Q.push(make_pair(c,make_pair(a,b)));
    }
    for(int i=0;i<n;i++)p[i]=i;
    int ans=0;
    while(!Q.empty()){
      int a=Q.top().second.first,b=Q.top().second.second;
      int cost=Q.top().first;Q.pop();
      int x=a,y=b;
      while(p[x]!=x)x=p[x];
      while(a!=x)a=p[a],p[a]=x;
      while(p[y]!=y)y=p[y];
      while(b!=y)b=p[b],p[b]=y;
      if(x!=y){
	ans+=cost;
	p[x]=y;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

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



// int N, M, A[10010], B[10010], COST[10010];
// pair<int, int> bridge[10010];
int P[10010];//村のrootが保存

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

int root(int a){//aのroot(代表元)を決める
  if(P[a]==a) return a;//aはroot
  return (P[a] = root(P[a]));//aの親のrootを求め,aの親とする
}
bool is_same_set(int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
   P[root(a)] = root(b);
}


int main(){
  int N, M, A[10010], B[10010], COST[10010];
  pair<int, int> bridge[10010];

  //root決める
  while(cin >> N >> M  && N>0){
    int allcost = 0;
    // int bridgenum = 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);
    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 (bridgenum == M-1)
      // {
      //   unite(a,b);
      //   // allcost += cost;
      // }
      if(is_same_set(a,b)){

      }else{
        unite(a,b);
        allcost += cost;
        // cout <<  allcost << endl;
        // bridgenum += 1;
      }
      // 「a から b に cost の橋がかかっている」と表示
    }
    cout <<  allcost << endl;
  }
}