Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class dsuClass {
- public:
- int n;
- vector<int> parent;
- vector<int> size;
- int sccCount;
- void init(int n) {
- this->n = n;
- parent.resize(n);
- size.resize(n);
- reset();
- }
- void reset() {
- sccCount = n;
- FOR (i, 0, n) parent[i] = i;
- FOR (i, 0, n) size[i] = 1;
- }
- int update(int a) {
- if (parent[a] == a) return a;
- return parent[a] = update(parent[a]);
- }
- void join(int a, int b) {
- a = update(a);
- b = update(b);
- if (size[b] > size[a]) swap(a, b);
- if (a == b) return;
- size[a] += size[b];
- parent[b] = a;
- sccCount--;
- }
- bool isMaster(int v) {
- return parent[v] == v;
- }
- void updateAll() {
- FOR (i, 0, n) update(i);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement