并查集 并与查:
查 :
int find(int x)
{
int t = x;
while(pre[t]!=t) /// 若子与父不同
{
t = pre[t]; /// 一直循环,直至相同。
}
int i,j;
while(pre[i] != t)
{
j = pre[i];
pre[i] = t;
t = j;
}
return t;
}
连 :
int union (int a,int b)
{
x = find[a];
y = find[b];
if(x != y)
pre[y] = x;
}