approach to New City

I consider two pairs for each node of tree, maximum distance and no. of ways to achieve that and similarly for second maximum distance. I calculate these from children in first dfs. After that, in second dfs, I update the node from it’s parent’s node. Is my approach wrong or my code is wrong?

//