Visited array for dfs
int [ ] vis = new int [ n ] ; for ( int k = 0 ; k < n ; k ++ ) { vis [ k ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { answer = Math . Max ( answer , dfs ( i , adj , vis ) ) ; } } return answer ; }