Runtime error(NZEC) on Operations on a Tree

Can someone please help on the following code, I am getting NZEC error. The problem link is - https://www.codechef.com/problems/TREEEE

def dfs(V,pV,cnt):
    if (Initial[V-1]==Target[V-1] and Active[V]==0) or (Initial[V-1]!=Target[V-1] and Active[V]==1):
        opr = 0
    else:
        cnt += 1
        opr = 1
    for i in adj[V]:
        if i==pV:
            continue
        Active[i] = Inactive[V]
        Inactive[i] = (Active[V]+opr)%2
        cnt += dfs(i,V,0)

    return cnt

n = int(input())
adj = [[] for _ in range(n+1)]
for _ in range(n-1):
    s,d = map(int,input().split())
    adj[s].append(d)
    adj[d].append(s)
Initial = list(map(int,input().split()))
Target = list(map(int,input().split()))
Active,Inactive = [0]*(n+1),[0]*(n+1)
print(dfs(1,0,0))

It is giving correct answer on following TCs :
I/P:
8
1 2
3 1
2 4
5 4
6 3
7 6
1 8
1 0 0 0 0 0 0 1
0 1 0 1 1 1 1 0
O/P
4

I/P
7
1 2
3 1
2 4
5 4
6 3
7 6
1 0 0 0 0 0 0
0 1 0 1 1 1 1
O/P
3

I/P
7
1 2
3 1
2 4
5 2
6 3
3 7
1 0 0 1 0 1 0
0 1 0 1 1 1 1
O/P
4

Please help me on which hidden TestCase it can give NZEC

@ssjgz , @l_returns , @vijju123 , @aryanc403 , @alei and @karangreat234 anyone please help on this solution. I am stuck over here since 2 days.
Will you please help on calling a counter variable in a recursive function and why this is giving NZEC I am thinking may be because of the counter variable.
Link of submitted solution : https://www.codechef.com/viewsolution/28001391
(Sorry to mention you here)

https://www.codechef.com/viewsolution/28002311
Here is your AC solution.
You need to set recursion limit in python if you recursion is gonna be deep enough.
I have just added line number 1 and line number 2

5 Likes

Thank you so much @l_returns :hugs:

2 Likes