Hi,
I am getting TLE at test case 6. I have added documentation to improve readability.
Things I have tried:
- Check for infinite loops : Not possible since I am keeping track of every node visited.
- Check time complexity : It is O(n), which should be enough for 2 second limit.
- Replace cin and cout with scanf and printf : Didn't work.
Thanks
You are passing vectors by value, which result in creation of vector in every function calls, making runtime be O(n2). Either pass it by reference or make them global.
Thanks Just_a_programmer! It is accepted now after making them global.
I will try to read up on these C++ details further. However, if passing by value was the concern, it should have been a memory issue (assuming that program was somehow crossing the memory limit) instead of execution time issue, right?
No. When you pass by value, the vectors (or arrays) are copied, and for large vectors, it'll take a lot of time and result in TLE!
When we pass by value vector is copied, it requires time as well as memory, but in most cases the memory limit is more then enough for the purpose.