We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a513631 commit ffad17cCopy full SHA for ffad17c
1 file changed
README.md
@@ -601,6 +601,38 @@ class UF {
601
}
602
603
604
+// another
605
+
606
+class UF {
607
+ constructor() {
608
+ this.root = {}
609
+ }
610
+ find(x) {
611
+ if (this.root[x] !== x) {
612
+ this.root[x] = this.find(this.root[x])
613
614
+ return this.root[x]
615
616
+ union(x, y) {
617
+ if(this.root[x] == null) this.root[x] = x
618
+ if(this.root[y] == null) this.root[y] = y
619
+ const xr = this.find(x)
620
+ const yr = this.find(y)
621
+ this.root[yr] = xr
622
623
+ getGroups() {
624
+ const g = {}
625
+ for(const [u, _] of Object.entries(this.root)) {
626
+ const r = this.find(u)
627
+ if(g[r] == null) g[r] = []
628
+ g[r].push(u)
629
630
+ return g
631
632
+}
633
634
635
636
class UnionFind {
637
constructor(n) {
638
this.parents = Array(n)
0 commit comments