MakeUnionFind(S)
is a constructor.
Find(s)
$s \in S$.
Union(u,v)
$u,v \in S$.
int component[i] = i
can be constructed in O(n)
Find(s) {return component[s]
is O(1)
Union(u,v)
for i = 1 to n if component[i] == component[u] component[i] = component[v]
vector<int> * component[i] = new vector<int>
Find(s) {return component[s]->first();}
Union(u,v)
- if (component[u]->size() > component[v]->size())
- swap (u,v)
- component[v]->append(component[u])
- tmp = component[u]
- for x in tmp
- x = component[v]
- delete tmp
Merge(u,v)
Find(u)