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<vector>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j;
  int b[100],m,n,x,y,z;
  int sm;
  vector<pair<int,pair<int,int> > > a;
  while(cin>>n>>m&&n+m){
    a.clear();
    for(i=0;i<m;i++){
      cin>>x>>y>>z;
      a.push_back(make_pair(z,make_pair(x,y)));
    }
    sort(a.begin(),a.end());
    memset(b,-1,sizeof(b));
    sm=0;
    for(i=0;i<m;i++){
      z=a[i].first;
      x=a[i].second.first;
      y=a[i].second.second;
      if(0){
      }else if(b[x]==-1&&b[y]==-1){
	b[x]=b[y]=i;
	sm+=z;
      }else if(b[x]==-1||b[y]==-1){
	b[x]=b[y]=max(b[x],b[y]);
	sm+=z;
      }else if(b[x]!=b[y]){
	for(j=0;j<n;j++){
	  if(j!=y&&b[j]==b[y])
	    b[j]=b[x];
	}
	b[y]=b[x];
	sm+=z;
      }
    }
    cout<<sm<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
int par[101],rank[101];
struct edge{int u,v,cost;};

using namespace std;

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

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

edge es[101];
int V,E;

int kruskal(){
  sort(es,es+E,comp);
  init_union_find(V);
  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(void){
  int i,a,b,c;

  while(cin >> V >> E){
    if(V==0 && E==0)break;
    for(i=0;i<E;i++){
      cin >> a >> b >> c;
      es[i].u=a;
      es[i].v=b;
      es[i].cost=c;
    }
    printf("%d\n",kruskal());
  }
  return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int h,i,j;
  int a[100][100],b[100],p[5000],q[5000],r[5000],m,n,o;
  int sm;
  while(cin>>n>>m&&n+m){
    memset(a,0,sizeof(a));
    for(i=0;i<m;i++){
      cin>>p[i]>>q[i]>>r[i];
      a[p[i]][q[i]]=r[i];
      a[q[i]][p[i]]=r[i];
    }
    do{
      h=0;
      for(i=1;i<m;i++){
	if(r[i-1]>r[i]){
	  swap(p[i-1],p[i]);
	  swap(q[i-1],q[i]);
	  swap(r[i-1],r[i]);
	  h=1;
	}
      }
    }while(h);
    memset(b,0,sizeof(b));
    h=-1;
    o=0;
    sm=0;
    for(i=1;i<n;i++){
      for(h++;r[h]==0;h++);
      sm+=r[h];
      if(b[p[h]]==0&&b[q[h]]==0){
	o++;
	b[p[h]]=o;
	b[q[h]]=o;
      }else if(b[p[h]]==0)
	b[p[h]]=b[q[h]];
      else if(b[q[h]]==0)
	b[q[h]]=b[p[h]];
      else{
	for(j=0;j<n;j++){
	  if(p[h]!=j&&b[p[h]]==b[j])
	    b[j]=b[q[h]];
	}
	b[p[h]]=b[q[h]];
      }
      for(j=0;j<n;j++){
	if(b[p[h]]==b[j]&&a[p[h]][j]){
	  a[p[h]][j]=0;
	  a[j][p[h]]=0;
	}
	if(b[q[h]]==b[j]&&a[q[h]][j]){
	  a[q[h]][j]=0;
	  a[j][q[h]]=0;
	}
      }
      for(j=h+1;j<m;j++){
	if(r[j]&&a[p[j]][q[j]]==0)
	  r[j]=0;
      }
    }
    cout<<sm<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF (1<<20)
typedef pair<int,int> P;

int N,M;
vector<P> bri[111];

int prim(){
  bool used[111];
  int ans = 0;
  memset(used,false,sizeof(used));
  priority_queue<P,vector<P>,greater<P> > que;
  que.push(P(0,0));
  while(!que.empty()){
    P p = que.top(); que.pop();
    int cost = p.first;
    int now = p.second;
    if(used[now]) continue;
    used[now] = true;
    ans += cost;
    for(int i=0;i<bri[now].size();i++){
      int next = bri[now][i].second;
      int to = bri[now][i].first;
      que.push(P(next,to));
    }
  }
  return ans;
}

int main(){
  while(cin >> N >> M ,N){
    for(int j=0;j<M;j++){
      int a,b,c;
      cin >> a >> b >> c;
      bri[a].push_back(P(b,c));
      bri[b].push_back(P(a,c));
    }
    cout << prim() << endl;
    for(int i=0;i<111;i++){
      bri[i].clear();
    }
  }
}