#define MAX 25005
typedef struct edge
{
int x, y;
int w;
}edge;
edge e[MAX];
int father[MAX], ranks[MAX];
bool cmp(edge a,edge b)
{
return a.w < b.w;
}
void Make_Set(int n)
{
for(int i = 1; i <= n; i++)
{
father[i] = i;
ranks[i] = 0;
}
}
int Find_Set(int x)
{
if(x != father[x])
father[x] = Find_Set(father[x]);
return father[x];
}
void Merge_Set(int x, int y)
{
x = Find_Set(x);
y = Find_Set(y);
if(x == y) return;
if(ranks[x] > ranks[y])
{
father[y] = x;
}
else if(ranks[x] < ranks[y])
{
father[x] = y;
}
else
{
ranks[y]++;
father[x] = y;
}
}
思想:贪心选择最小的w值,其他的就是并查集的利用了。
克鲁斯卡尔算法模版
最新推荐文章于 2022-08-01 11:45:47 发布