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:

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 -

Use this class for fast input instead of scanner/bufferedReader

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.