POJ 2395
//最小生成树求生成树中的最大边,使用Kruskal算法
//总结:re错误为开的数组太小
#include <stdio.h>
#include <algorithm>
using namespace std;
#define nodesize 2001
#define edgesize 10001
typedef struct node
{
int v; //起点
int u; //终点
__int64 w; //权值
}node;
node edges[edgesize]; //边
node record[edgesize]; //最小生成树中的边
int final[nodesize]; //每一节点的双亲节点
int nodes[nodesize]; //以该节点为根的树的节点个数
bool cmp(node a,node b)
{
return (a.w <b.w);
}
int findroot(int n) //查找根节点
{
if(final[n] == n)
return n;
else
{
final[n] = findroot(final[n]);
}
return final[n];
}
int main()
{
int N,M;
//freopen("1.txt","r",stdin);
while(scanf("%d%d",&N,&M)!=EOF)
{
int start,end;
__int64 w;
for(int i=1;i<M+1;++i)
{
scanf("%d%d%I64d",&start,&end,&w);
edges[i].v = start;
edges[i].u = end;
edges[i].w = w;
}
//构造N棵子树
for(int i1=1;i1<N+1;++i1)
{
final[i1] = i1;
nodes[i1] = 1;
}
//对边进行排序
sort(edges+1,edges+M+1,cmp);
int flag = 0;
__int64 maxsize = 0;
//遍历所有的边
for(int i2=1;i2<M+1;++i2)
{
int a = edges[i2].v;
int b = edges[i2].u;
__int64 w = edges[i2].w;
int roota = findroot(a);
int rootb = findroot(b);
if(roota!=rootb)
{
flag++;
record[flag].v = a;
record[flag].u = b;
record[flag].w = w;
if(w > maxsize)
{
maxsize = w;
}
if(nodes[roota]>=nodes[rootb])
{
final[rootb] = roota;
nodes[roota] += nodes[rootb];
}
else
{
final[roota] = rootb;
nodes[rootb] += nodes[roota];
}
}
}
printf("%d\n",maxsize);
}
return 0;
}