The content of this website is only deployed under the domain
𝙨𝙘𝙤𝙩𝙩𝙮𝙚𝙪𝙣𝙜.𝙩𝙤𝙥(scottyeung[dot]top). Any other domain sites are unauthorized
illegal mirrors.
intFind(intx)//使用递归写 find 函数,同时有路径压缩
{inta;a=x;while(set[a]!=a)///循环方法查找前导点
{a=set[a];}inti=x,j;while(i!=a)///路径压缩,修改历经的前导点
{j=set[i];///记录 x 的前导结点
set[i]=a;///将 i 的前导结点设置为 r 的根节点。
i=j;}returna;}voidUnion(intx,inty){inta,b;a=Find(x);///x 的根节点为 a
b=Find(y);///y 的根节点为 b
if(a!=b)///如果 a,b 不是相同的根节点,则说明 ab 不是连通的
{set[a]=b;///将 a,b 连接,将 a 的前导点设置为 b
}}