【模板】并查集
1 int find(int x) 2 { 3 int r = x; 4 while(father[r]!=r) 5 r = father[r]; 6 return r; 7 } 8 /* 9 int find(int x) 10 { 11 if(father[x] == x) 12 return x; 13 else 14 return father[x] =find(father[x]); 15 } 16 */ 17 18 void join(int x,int j) 19 { 20 int fx = find(x),fy = find(y); 21 if(fx!=fy) 22 father[fx] = fy; 23 } 24 25 void Union(int x,int y) 26 { 27 int rx,ry; 28 rx = find(x); 29 ry = find(y); 30 father[rx] = ry; 31 }
时间: 2024-11-06 23:11:01