disjoint_set.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <iostream>
  2. #include "disjoint_set.hpp"
  3. #define endl '\n'
  4. using namespace std;
  5. void print(DisjointSet *ds)
  6. {
  7. cout << ds->root(0) << " " << ds->size(0) << " " << ds->root(1) << " " << ds->size(1) << endl;
  8. }
  9. int main()
  10. {
  11. ios_base::sync_with_stdio(0);
  12. cin.tie(0);
  13. // Creates a disjoint set that allows undo last action.
  14. // It creates a separate array history to allow this.
  15. auto ds = disjoint_set()->with_undo()->init(2);
  16. print(ds);
  17. // 0 | root 0 | size 1
  18. // 1 | root 1 | size 1
  19. ds->merge(0, 1);
  20. print(ds);
  21. // 0 | root 1 | size 2
  22. // 1 | root 1 | size 2
  23. ds->undo();
  24. print(ds);
  25. // 0 | root 0 | size 1
  26. // 1 | root 1 | size 1
  27. // This disjoint set doesn't allow to undo (it doesn't build history vector)
  28. // but has the same base type that the former disjoint set. It can be passed
  29. // to the same function and they expose the same api ...
  30. // with some caveat see below
  31. ds = disjoint_set()->init(2);
  32. print(ds);
  33. // 0 | root 0 | size 1
  34. // 1 | root 1 | size 1
  35. ds->merge(0, 1);
  36. print(ds);
  37. // 0 | root 1 | size 2
  38. // 1 | root 1 | size 2
  39. // Caveat: This disjoint expose undo function, but it will throw an error if
  40. // used since it is not really available.
  41. // ds->undo();
  42. // print(ds);
  43. return 0;
  44. }