最小生成树
最小生成树
Kruskal算法
//使用并查集,基本操作略
Quick_sort(1,m); //对边按权值排序
For i:=1 to m do
Begin
X:=father(e[i].a);
Y:=father(e[i].b);
If x<>y then
Begin
Union(x,y);
Inc(k);
E[i].select:=true;
End;
If k=n-1 then break;
End;
//使用并查集,基本操作略
Quick_sort(1,m); //对边按权值排序
For i:=1 to m do
Begin
X:=father(e[i].a);
Y:=father(e[i].b);
If x<>y then
Begin
Union(x,y);
Inc(k);
E[i].select:=true;
End;
If k=n-1 then break;
End;