Skip to content

Commit ffad17c

Browse files
authored
Update README.md
1 parent a513631 commit ffad17c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,38 @@ class UF {
601601
}
602602
}
603603

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+
// another
635+
604636
class UnionFind {
605637
constructor(n) {
606638
this.parents = Array(n)

0 commit comments

Comments
 (0)