kruskal c++

C++
int find(int x) {
  while ( x != link[x]) x = link[x];
  return x;
}

int unite(int a, int b) {
  if (size[a] > size[b]) swap(a, b);
  size[a] += size[b];
  link[b] = a;
}

int kruskal() {
  int link[N+1], size[N+1];
  sort(adj.begin(), adj.end());
  for (int i=0; i<N; i++) {
    link[i] = i;
    size[i] = 1;
  }
  int cnt = 0;
  for (int i=0; i<adj.size(); i++) {
    int a = find(adj[i].second.first);
    int b = find(adj[i].second.second);
    if (a != b) { unite(a, b); cnt += adj[i].first; }
  }
  return cnt;
}
Source

Also in C++: