loop for all neighbors except parent and aggregate sum over all subtrees
for ( int i = 0 ; i < edge [ u ] . size ( ) ; i ++ ) { int v = edge [ u ] [ i ] ; if ( v != parent ) { dfs ( v , u , totalSum , edge , subtree , res ) ; sum += subtree [ v ] ; } }