Add edges
for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i , 0 ] ; int v = edges [ i , 1 ] , w = edges [ i , 2 ] ; adj [ u ] . Add ( new pair ( v , w ) ) ; adj [ v ] . Add ( new pair ( u , w ) ) ; } dfs ( R , 0 , K ) ;