import { fillRange } from "@thi.ng/arrays/fill-range" ; remarks -  https * -  https export class DisjointSet { roots : Uint32Array ; ranks : Uint8Array ; count : number ; param n constructor ( n ) { this . roots = fillRange ( new Uint32Array ( n ) ) ; this . ranks = new Uint8Array ( n ) ; this . count = n ; } param id canonical ( id ) { const roots = this . roots ; while ( id !== roots [ id ] ) { id = roots [ id ] = roots [ roots [ id ] ] ; } return id ; } param a param b union ( a , b ) { const rootA = this . canonical ( a ) ; const rootB = this . canonical ( b ) ; if ( rootA === rootB ) { return rootA ; } this . count -- ; const ranks = this . ranks ; const ra = ranks [ rootA ] ; const rb = ranks [ rootB ] ; if ( ra < rb ) { return ( this . roots [ rootA ] = rootB ) ; } ra === rb && ranks [ rootA ] ++ ; return ( this . roots [ rootB ] = rootA ) ; } param a param b unified ( a , b ) { return this . canonical ( a ) === this . canonical ( b ) ; } remarks If only the number of subsets is required, use the `count`
     * property of this class instance instead (O(1), updated with each
     * call to  DisjointSet . union ).
      subsets ( ) { const sets = new Map ( ) ; const roots = this . roots ; for ( let i = roots . length ; i -- > 0 ; ) { const id = this . canonical ( i ) ; const s = sets . get ( id ) ; if ( s ) { s . push ( i ) ; } else { sets . set ( id , [ i ] ) ; } } return sets ; } } /**
 * Creates a new  DisjointSet with capacity `n`.
 *
 *  param n export const defDisjointSet = ( n ) => new DisjointSet ( n ) ;