DFS fast in python

I was trying to solve FIRESC using CodeChef: Practical coding for everyone. Have used dfs implemented using stack in python and sys.stdin.readlines(). Yet, it is slow. Can someone suggest me if there is a faster way ? as here N=10^5, and it becomes slow for that.