Push every node to the queue which has no incoming edge
for ( int i = 0 ; i < V ; i ++ ) { if ( indeg [ i ] == 0 ) q . push ( i ) ; } vector < int > res ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; res . push_back ( u ) ;