模版 并查集

 1 递归形式
2
3 int find(int x)
4 {
5 if(x!=f[x])f[x]=find(f[x]);
6 return f[x];
7 }
8
9
10 非递归
11 int find(int x)
12 {
13 if(f[x]==x)return x;
14 int r=x;
15 while(r!=f[r])
16 {
17 r=f[r];
18 }
19 int y=x,k;
20 while(y!=f[y])
21 {
22
23 k=f[y];
24 f[y]=r;
25 y=k;
26
27
28 }
29
30
31 return f[x];
32
33 }
34   
原文地址:https://www.cnblogs.com/acSzz/p/2398714.html