Fire Escape Routes (NZEC)

Please help me anyone why am i getting NZEC

import sys
li  =   lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw  =   lambda : sys.stdin.readline().strip().split()
ni  =   lambda : int(sys.stdin.readline().strip())
nsi =   lambda : sys.stdin.readline().strip()
mi  =   lambda : map(int,sys.stdin.readline().split())

from collections import defaultdict as df
from math import *

t=ni()
for _ in range(t):
    d=df(list)
    n,m=mi()
    for i in range(m):
        u,v=mi()
        d[u].append(v)
        d[v].append(u)
    
    visited=[0]*(n+1)
    cnt=0
    res=1
    def dfs(node,d,visited):
        global cc_count
        visited[node]=1
        cc_count+=1
        for i in d[node]:
            if(visited[i]==0):
                dfs(i,d,visited)
            
    for i in range(1,n+1):
        if(visited[i]==0):
            cc_count=0
            dfs(i,d,visited)
            res=(res*cc_count)%1000000007
            cnt+=1
    print(cnt,res)

Python’s Default recursion limit is 1000. So when you are making a call to dfs so it may exceed the limit.
Here is AC link added one line that is the recursion limit is set according to the given constraints: https://www.codechef.com/viewsolution/37548385

1 Like

Thank you so much . Appreciate your help.

1 Like