Created
October 28, 2021 17:09
-
-
Save ddoronin/4b194e384e587f9fc14c43009e87beb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DSU { | |
private p: number[]; | |
constructor(n: number) { | |
this.p = new Array(n).fill(0).map((_, i) => i); | |
} | |
find(x: number) { | |
while(this.p[x] !== x) { | |
x = this.p[x]; | |
} | |
return x; | |
} | |
union(x: number, y: number) { | |
this.p[this.find(x)] = this.find(y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment