Comments

another solution for problem E. https://codeforces.me/contest/2285/submission/398683302

Real Code

In this code, we use the structure like LCA with binary-lifting, and the time Complexity is $$$ O(NK\log K) $$$. This is first coded by @sadmecurial, but this user is unfortunately disabled.