int father[105]; //根據需要自己開數組
//father數組的值是每個下標的老大 father[5]=1 表示的是5的老大是1
int findf(int x)//找老大函數
{
int a=x;
while(x!=father[x])
{
x=father[x];
}
//路徑壓縮
while(a!=father[a])
{
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union(int a,int b) //合併函數
{
int x,y;
x=findf(a);
y=findf(b);
if(x!=y)
{
father[x]=y;
}
}
並查集模板(找老大函數)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.