queue to do BFS .
queue < int > Q ; distance [ u ] = 0 ; Q . push ( u ) ; visited [ u ] = true ; while ( ! Q . empty ( ) ) { int x = Q . front ( ) ; Q . pop ( ) ; for ( int i = 0 ; i < edges [ x ] . size ( ) ; i ++ ) { if ( visited [ edges [ x ] [ i ] ] ) continue ;