Common and check set. Combined averaging O (α (n)) query averaging O (α (n))
1 //Common versions2 3 //Union Find4 intf[1005000];5 6 voidINIT (intsize)7{ for(intI=0; i<=size;i++) f[i]=i;}8 9 intFINDF (intx)Ten{returnF[x]==x? x:f[x]=findf (f[x]);}; One A intConnectintx) -{F[FINDF (a)]=findf (b);}
You can split and check the set. Worst case: Merge O (n) query O (n) split O (n) Average case: Merge O (logn) query O (logn) split O (Logn)
1 //Detachable version2 //Union Find3 intf[1005000];4 5 voidINIT (intsize)6{ for(intI=0; i<=size;i++) f[i]=i;}7 8 intFINDF (intx)9{ for(; x!=f[x];x=f[x]); }Ten One voidSetroot (intx) A{if(f[x]!=x) Setroot (f[x]); f[x]=f[f[x]]=x;} - -InlinevoidLinkintXinty) the{setroot (x); f[x]=y;} - -InlinevoidCutintXinty) -{setroot (x); f[y]=y;}
You can see behind this comparison like LCT ...
And check set, and can be split and check