Replace each vertex with it 's  corresponding parent except  the root vertex
if ( parent [ Vertices [ k ] ] != 0 ) { Vertices [ k ] = parent [ Vertices [ k ] ] ; } } let ans = true ; let flag ; for ( let k = 0 ; k < V ; k ++ ) {