You’re using recursive DFS, which overflows the recursion stack. Your code gets AC with this modification:
from sys import setrecursionlimit
setrecursionlimit(10000)
I’d recommend avoiding recursion wherever possible if you want to pass time limits with Python here.
On a sidenote, your code is not pythonic and ugly stylewise. For example, arrays can be initiated faster and elegantly:
graph = [[] for _ in range(n)]
visited = [0] * n # only with immutable element types
Use spaces for readability and variable names that mean something (temp what? flag of what? array of what?). In interpreted languages, albeit not only, decent style translates to performance, and you’re going to need it on SPOJ. Also learn fast I/O which is especially important with graph problems.