This problem looks like a fairly standard tree DP problem, I won't spoil the solution in case people want to think about it.
However, the reason why this problem is interesting (or perhaps extremely painful) is that the memory limit is 32MB. Given that $$$N$$$ can be 1.5 million, this means you're not even allowed to store $$$6N$$$ 32-bit integers.
Therefore, it seems that the main challenge of this problem is to be able to fit everything inside such a tight memory limit. Notably, I've gotten MLE just trying to read in the input and trying to get the tree stored in a representation that makes it possible for me to start working on the DP part of the problem.
In the modern era, memory limits are no longer this tight, but a lot of problems on szkopul retain these tight memory limits which occasionally makes it an interesting (or perhaps extremely painful) challenge to figure out how to fit things in the memory limit.
What's intended here? I asked about this problem in the AC server and got some advice with things such as relabeling the tree with ETT or simulating DFS without an explicit stack. It feels like the authors intended for some precise $$$5N + \mathcal{O}(1)$$$ memory solution and it's a little alarming that I can't even get beyond "read the input and come up with some representation for the tree" without getting MLE.