For the problem : https://codeforces.net/problemset/problem/1280/C My submission is giving Runtime error on test 4. Exit code is 1. Please help me find my mistake. From what I understood reading other posts the exit code 1 is due to input issues but since I am getting it at test case 4 I don't know where is the problem.
def dfs(mat,subtree_sizes,p,v,good,bad,k):
for i in range(len(mat[p])):
x=mat[p][i][0]
d=mat[p][i][1]
if v[x]==0:
v[x]=1
dfs(mat,subtree_sizes,x,v,good,bad,k)
bad[0]+=d*min(subtree_sizes[x],2*k-subtree_sizes[x])
good[0]+=d*(subtree_sizes[x]%2)
subtree_sizes[p]+=subtree_sizes[x]
return 0
for _ in range(int(input())):
k=int(input())
mat=[[] for i in range(2*k)]
v=[0 for i in range(2*k)]
subtree_sizes=[1 for i in range(2*k)]
good=[0]
bad=[0]
for j in range(2*k-1):
a,b,d=map(int,input().split())
mat[a-1].append((b-1,d))
mat[b-1].append((a-1,d))
v[0]=1
dfs(mat,subtree_sizes,0,v,good,bad,k)
print(good[0],bad[0])
Same,I got RE on dfs problems using python. I think it's python's fault. After changing the solutions into C++, they all accepted.