Recently in Educational Codeforces round 46 I wrote the following programme for 1000E - We Need More Bosses in Java:
Now I am pretty sure that the program runs in O(n + m) time(it is a dfs plus an iteration over the edges). However, the program got rejected due to Time Limit Exceeded despite its optimal asymptotic efficiency. Would anyone point out which part of my programme is consuming lots of time? I am a newbie in programming and really appreciate any help!
Update: Thank you all for giving me advice! I have solved the problem.
Use faster input routines due to massive input (~10^6 integers).
Thank you very much!
Scanner is too slow in Java for competitive programming problems, replace it with a buffered reader, or use a custom written scanner.
Thank you very much!
Slow I/O
I just copied Petr's Java template and it passed.
Thank you very much!
switch to C++
Auto comment: topic has been updated by Tom--CCS (previous revision, new revision, compare).
LoOoOOL. The fact that you survived for so long and even became purple while using Scanner in Java is more mysterious than the running speed of Java itself.
Now he has fast Scanner and he became orange