Using recursion formula to calculate the values of lca [ ] [ ] and dist [ ] [ ]
lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] NEW_LINE dist [ node ] [ i ] = ( dist [ node ] [ i - 1 ] + dist [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) NEW_LINE for i in graph [ node ] : NEW_LINE if ( i [ 0 ] == parent ) : NEW_LINE continue NEW_LINE dfs ( i [ 0 ] , node , h + 1 , i [ 1 ] ) NEW_LINE