1. 程式人生 > >【模板】並查集

【模板】並查集

int find(int x)  
{  
    int r = x;  
    while(father[r]!=r)  
    r = father[r];  
    return r;  
}  
/* 
int find(int x) 
{ 
    if(father[x] == x) 
    return x; 
    else 
    return father[x] =find(father[x]); 
} 
*/  
  
void join(int x,int j)  
{  
    int fx = find(x),fy = find(y);  
    if(fx!=fy)  
    father[fx] = fy;  
}  
  
void Union(int x,int y)  
{  
    int rx,ry;  
    rx = find(x);  
    ry = find(y);  
    father[rx] = ry;  
}