Data Structures/Disjoint Set (Union Find).cpp
// Connect two nodes - returns true if connected, false if already connected
Connect two nodes - returns true if connected, false if already connected
Connect two nodes - returns true if connected false if already connected
bool connect(int x, int y)
{
    int xroot = root(x);
    int yroot = root(y);
    if(xroot == yroot)
        return false;
    if( uf[xroot].rnk < uf[yroot].rnk )
        uf[xroot].p = yroot;
    else if( uf[yroot].rnk < uf[xroot].rnk )
        uf[yroot].p = xroot;
    else
        uf[yroot].p = xroot, uf[xroot].rnk++;
    return true;
}
bool connect ( int x , int y ) { int xroot = root ( x ) ; int yroot = root ( y ) ; if ( xroot == yroot ) return false ; if ( uf [ xroot ] . rnk < uf [ yroot ] . rnk ) uf [ xroot ] . p = yroot ; else if ( uf [ yroot ] . rnk < uf [ xroot ] . rnk ) uf [ yroot ] . p = xroot ; else uf [ yroot ] . p = xroot , uf [ xroot ] . rnk ++ ; return true ; }
Connect two nodes - returns true if connected, false if already connected
Connect two nodes - returns true if connected false if already connected
// Union by rank