Union-find Template

 

Union-find can be used to find if two elements are in the same set in constant time.

Union-find template

  1. path compression
  2. union by rank

Without 2, the query time is O(logn) With 2, the query time is O(1)

// TODO