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 ] ; } } for ( auto i : graph [ node ] ) { if ( i . first == parent ) continue ; dfs ( i . first , node , h + 1 , i . second ) ; } }