Problem link : contest
practice
Difficulty : Easy - Medium
Pre-requisites : graphs, trees, in order
Solution :
This kind of problems are very standard. You can have a look at following problem and a very nicely written editorial of elfus0.
Unfortunately test data was weak which led to passing of unoptimized solution. We will update the test cases soon.
Setter’s solution: link