TLE In PERCAPTA

I am getting a TLE error for this question although my approach is very similar to that given in the editorial.
In my case the time complexity of the code is O(nlogn), which i think should run within 1sec (Given time limit) for the constraint: 1<=N<=2*10^5

Please let me know where am i going wrong

https://www.codechef.com/viewsolution/34631271

try using a dictionary to store if if i-th node is max or not, and then instead of fil.sort() and binary search you can simply write

if max_[u] == true :

UPDATE:
I tested it for you : WA

edit : your code is not considering the case where more than 3 nodes all from different levels are connected to satisfy chef land’s king.

1
20 19
1 2 2 1 1 1 1 2 1 1 2 1 2 1 2 1 2 2 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 2
1 11
1 8
11 6
11 17
2 7
2 14
6 4
17 15
17 5
4 10
15 12
15 18
15 16
15 9
5 13
16 20
9 19
13 3

Your output :
3
15 17 18

Expected output:
4
11 15 17 18

1 Like

Thanks for your reply.
It was really helpful :grinning:

1 Like