#pragma once namespace cgv{ namespace math{ /** * A union find data structure */ struct union_find { int* sz; int* id; int components; int N; ///N number of all elements union_find(int N):N(N) { components=N; id = new int[N]; sz = new int[N]; for(int i = 0; i < N;i++) { id[i] = i; sz[i] = 1; } } ///destructor ~union_find() { delete[] sz; delete[] id; } ///number of sets (initially number of all elements) int num_of_components() { return components; } //return the number of elements in the set which contains x int num_in_set(int x) { int l= find(x); return sz[l]; } //retruns label of the first set this can be used in combination with next_set to iterate over all sets int first_set() { int x =0; while(x < N && x != id[x]) x++; return x; } //returns the next set of x or -1 if x is the last one int next_set(int x) { x++; while(x