Run Error and TLE in PSHTTR (Help Needed)

I have implemented the editorial of PISHTY AND TREE step by step as it is in JAVA. However I get tle in one case of advanced(70 points) and run error in the other.First 30 points works correctly.

Here is the link of my solution:
https://www.codechef.com/viewsolution/14612335

Link of question:

Where am I going wrong?

Update: Run Error has been rectified. Still getting TLE.

Update: Got AC(100 points) . Works correctly now

You can get rid of TLE error by using fast IO
.But I am facing problem with RE on last subtask too. Will be wathcing this question for a solution.

My solution - https://www.codechef.com/viewsolution/14616417

Use this class for fast input instead of scanner/bufferedReader
https://gist.github.com/akmittal006/5f154c4b75d6ec2d672106ae17b1c969

I got rid of run error. It was a stack overflow error due to excessive recursion calls in my dfs function

I used the given fast input. My last subtask works using it. But I am still geting TLE for second last subtask.

Why should it take so much time in the first place?
The complexity of my code is O(NlogN)

I used a faster output ( PrintWriter Class ) . It worked comfortably using that. Thanks for the help.

//