Python 2.7 显示运行时错误的在线判断

Python 2.7 显示运行时错误的在线判断,python-2.7,runtime-error,depth-first-search,Python 2.7,Runtime Error,Depth First Search,我的代码在所有测试用例中都运行良好。然而,当我将其提交给SPOJ时,它显示了NZEC运行时错误 可能是堆栈溢出造成的。因为测试用例可能有2000个节点 看看这个可能是堆栈溢出造成的。因为测试用例可能有2000个节点 看到这个了吗 def dfs(graph, start, visited = None, parent = None): if visited == None: visited = set() visited.add(start) f

我的代码在所有测试用例中都运行良好。然而,当我将其提交给SPOJ时,它显示了NZEC运行时错误


可能是堆栈溢出造成的。因为测试用例可能有2000个节点


看看这个

可能是堆栈溢出造成的。因为测试用例可能有2000个节点

看到这个了吗

def dfs(graph, start, visited = None, parent = None):
    if visited == None:
        visited = set()
        visited.add(start)
    for num in graph[start]:
        if num not in visited:
            visited.add(num)
            if dfs(graph,num,visited,start):
                return True
        else:
            if num != parent:
                return True
    return False
tc = int(raw_input())
for i in range(1, tc+1):
    yo = {}
    s = raw_input()
    n, r = map(int, s.split())
    for j in range(r):
        s = raw_input()
        p, q = map(int, s.split())
        yo[p] = yo.get(p,[]) + [q]
        yo[q] = yo.get(q,[]) + [p]
    start = q
    if dfs(yo, start):
        print "Scenario #%d:" %(i)
        print "Suspicious bugs found!"
    else:
        print "Scenario #%d:" %(i)
        print "No suspicious bugs found!"