/**
并查集实现克鲁斯卡尔算法
6
1 2 6
2 4 1
1 4 2
1 3 1
3 5 4
4 5 3
**/
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX_N 100
using namespace std;
struct Node
{
    int numr;
    int numd;
    int val;
};
int cmp(Node a,Node b)
{
    return a.val<b.val;
}
vector<Node> v;
int F[MAX_N];
int findPar(int n)
{
    if(F[n]==n)
    {
        return F[n];
    }
    else
    {
        F[n]=findPar(F[n]);
        return F[n];
    }
}
int merges(int a,int b)
{
    int x=findPar(a);
    int y=findPar(b);
    if(x==y)
    {
        return 1;
    }
    else
    {
        F[y]=x;
        return 0;
    }
}
int main()
{
    for(int i=0;i<MAX_N;i++)
    {
        F[i]=i;
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        Node nod;
        cin>>nod.numr>>nod.numd>>nod.val;
        v.push_back(nod);
    }
    sort(v.begin(),v.end(),cmp);
    for(int i=0;i<v.size();i++)
    {
        if(!merges(v[i].numr,v[i].numd))
        {
            v1.push_back(v[i]);
        }
    }
    return 0;
}
原文:http://www.cnblogs.com/NarcissusBlog/p/5860679.html