feat: add UFDS flashcards and project learnings infrastructure
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#+title: Udfs
|
||||
* impl
|
||||
#+begin_src cpp
|
||||
#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;
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user