BFS function to find minimum distance
void bfs ( bool visit [ ] , int dist [ ] , queue < int > q ) { while ( ! q . empty ( ) ) { int temp = q . front ( ) ; q . pop ( ) ; for ( int i = 0 ; i < g [ temp ] . size ( ) ; i ++ ) { if ( visit [ g [ temp ] [ i ] ] != 1 ) { dist [ g [ temp ] [ i ] ] = min ( dist [ g [ temp ] [ i ] ] , dist [ temp ] + 1 ) ; q . push ( g [ temp ] [ i ] ) ; visit [ g [ temp ] [ i ] ] = 1 ; } } } }