Using recursion formula to calculate the values of lca [ , ] and dist [ , ]
lca [ node , i ] = lca [ lca [ node , i - 1 ] , i - 1 ] ; dist [ node , i ] = dist [ node , i - 1 ] + dist [ lca [ node , i - 1 ] , i - 1 ] ; } } foreach ( int [ ] i in graph [ node ] ) { if ( i [ 0 ] == parent ) continue ; dfs ( i [ 0 ] , node , h + 1 , i [ 1 ] ) ; } }