Adjacency list to store neighbors of each array element
for ( auto & it : A ) { mp [ it . first ] . push_back ( it . second ) ; mp [ it . second ] . push_back ( it . first ) ; } auto it = mp . begin ( ) ;