Files
cpp-flashcards/org/study_deck_02/dsa/udfs.org
T
tomatocream dfadc1ca48 refactor: move DSA notes to org/study_deck_02/
Restructure NeetCode study materials into a dedicated Anki-exportable
deck directory. Separates DSA problem notes from C++ flashcards.

- Move roadmap.org from leetcode/out/ to org/study_deck_02/
- Move 200 note files from org/cpp/dsa/ to org/study_deck_02/dsa/
- Add #+PROPERTY: STUDY_DECK_02 to all org files for org-anki export
- Simplify roadmap format: properties drawer replaces nested headings
- Update NEETCODE backlinks in all note files
- Update extract.mjs and scaffold-notes.mjs for new paths
- Create org/study_deck_02/AGENTS.md with study workflow docs
- Update root and leetcode AGENTS.md
2026-06-01 16:12:21 +08:00

948 B

Udfs

impl

#include <vector>
#include <numeric>
#include <algorithm>
class Ufs {
  private:
  std::vector<int> p;
  std::vector<int> s;
  std::vector<int> r;
  public:
  Ufds(int n) {
    p.resize(n);
    std::iota(p.begin(), p.end(), 0);
    s.assign(n, 1); // test on equality with the assign fill and iota explain the difference
    std::fill(r.begin(),r.end(),0);
    numSets = n;
  }
  ~Ufds() {}
  ~Ufds() = default;
  int find(int x) {
    if (p[x] == x) return x;
    return p[x] = find(p[x]);
  }
  int find(int x) {
    int px = p[x];
    if (px != x) {
      px = find(px);
    }
    p[x] = px;
    return px;
  }
};

should ask what new returns? delete? what happens? why are we deleting it ? heap allocated? should show equivalent version of using std::array to vector should ask what resize does?

what about a true dynammic version where we create it upon calling find()