Adjacency list to store neighbors of each array element
foreach ( Pair it in A ) { List < int > temp ; temp = ( mp . ContainsKey ( it . first ) ) ? mp [ it . first ] : new List < int > ( ) ; temp . Add ( it . second ) ; if ( ! mp . ContainsKey ( it . first ) ) mp . Add ( it . first , temp ) ; else mp [ it . first ] = temp ; temp = ( mp . ContainsKey ( it . second ) ) ? mp [ it . second ] : new List < int > ( ) ; temp . Add ( it . first ) ; if ( ! mp . ContainsKey ( it . second ) ) mp . Add ( it . second , temp ) ; else mp [ it . second ] = temp ; } int It = 0 ;