Skip to content

Commit 7e8ca09

Browse files
authored
Update Disjoint Set Union.cpp
1 parent da1a504 commit 7e8ca09

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Disjoint Set Union.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,28 @@ void unite(int a, int b) {
2323
}
2424
}
2525

26+
27+
class dsu {
28+
public:
29+
vector<int> p;
30+
int n;
31+
32+
dsu(int _n) : n(_n) {
33+
p.resize(n);
34+
iota(p.begin(), p.end(), 0);
35+
}
36+
37+
inline int get(int x) {
38+
return (x == p[x] ? x : (p[x] = get(p[x])));
39+
}
40+
41+
inline bool unite(int x, int y) {
42+
x = get(x);
43+
y = get(y);
44+
if (x != y) {
45+
p[x] = y;
46+
return true;
47+
}
48+
return false;
49+
}
50+
};

0 commit comments

Comments
 (0)