Find finds the root of the set to which the given element belongs. It performs path compression to make future Find operations faster.
(q int)
| 35 | // Find finds the root of the set to which the given element belongs. |
| 36 | // It performs path compression to make future Find operations faster. |
| 37 | func (u *UnionFind) Find(q int) int { |
| 38 | if q != u.parent[q] { |
| 39 | u.parent[q] = u.Find(u.parent[q]) |
| 40 | } |
| 41 | return u.parent[q] |
| 42 | } |
| 43 | |
| 44 | // Union merges the sets, if not already merged, to which the given elements belong. |
| 45 | // It performs union by rank to keep the tree as flat as possible. |
no outgoing calls