Problem p02762 (python)

Fixed submission:
N,M,K = list(map(int,input().split(' ')))

friend_list = [0]*(N+1)
bad_list = [0]*(N+1)
group = {}

parent = [i for i in range(N+1)]

def find(i):
    rireki = []
    while parent[i] != i:
        rireki.append(i)
        i = parent[i]
    for r in rireki:
        parent[r]=i
    return i

def union(i, j):
    i = find(i)
    j = find(j)
    parent[j] = i



for i in range(M):
    A,B = list(map(int,input().split(' ')))
    friend_list[A]+=1
    friend_list[B]+=1
    
    union(A,B)

for i in range(1,N+1):
    oya = find(i)
    parent[i] = oya
    #print(oya)
    if oya in group:
        group[oya] = group[oya]+1
    else:
        group[oya]=1

for i in range(K):
    A,B = list(map(int,input().split(' ')))
    if find(A)==find(B):
        bad_list[A]+=1
        bad_list[B]+=1
    
    #union(A,B)



#print(parent)
#print('parent')

#print('friend')
#print(friend_list)
#print('bad')
#print(bad_list) 
#print(group)

kouho = [0]*N

for i in range(1,N+1):
    oya = find(i)
    abc = group[oya]-friend_list[i]-bad_list[i]-1
    kouho[i-1] = abc

for k in kouho:
    print(k,end=" ")
print()

#print(parent)

Buggy submission (verdict: Runtime Error):
    import sys
    sys.setrecursionlimit(5000)
     
    N,M,K = list(map(int,input().split(' ')))
     
    friend_list = [0]*(N+1)
    bad_list = [0]*(N+1)
    group = {}
     
    parent = [i for i in range(N+1)]
     
    def union(x,y):
        x = find(x)
        y = find(y)
        parent[x] = y
     
    def find(x):
        if parent[x] == x:
            return x
        else:
            parent[x]= find(parent[x])
            return parent[x]
     
     
     
    for i in range(M):
        A,B = list(map(int,input().split(' ')))
        friend_list[A]+=1
        friend_list[B]+=1
        
        union(A,B)
     
    for i in range(1,N+1):
        oya = find(i)
        parent[i] = oya
        #print(oya)
        if oya in group:
            group[oya] = group[oya]+1
        else:
            group[oya]=1
     
    for i in range(K):
        A,B = list(map(int,input().split(' ')))
        if find(A)==find(B):
            bad_list[A]+=1
            bad_list[B]+=1
        
        #union(A,B)
     
     
     
    #print(parent)
    #print('parent')
     
    #print('friend')
    #print(friend_list)
    #print('bad')
    #print(bad_list) 
    #print(group)
     
    kouho = [0]*N
     
    for i in range(1,N+1):
        oya = find(i)
        abc = group[oya]-friend_list[i]-bad_list[i]-1
        kouho[i-1] = abc
     
    for k in kouho:
        print(k,end=" ")
    print()
     
    #print(parent)

Buggy submission (verdict: Runtime Error):
    import sys
    sys.setrecursionlimit(5000)
     
    N,M,K = list(map(int,input().split(' ')))
     
    friend_list = [0]*(N+1)
    bad_list = [0]*(N+1)
    group = {}
     
    parent = [i for i in range(N+1)]
     
    def union(x,y):
        x = find(x)
        y = find(y)
        parent[x] = y
     
    def find(x):
        if parent[x] == x:
            return x
        else:
            parent[x]= find(parent[x])
            return parent[x]
     
     
     
    for i in range(M):
        A,B = list(map(int,input().split(' ')))
        friend_list[A]+=1
        friend_list[B]+=1
        
        union(A,B)
     
    for i in range(1,N+1):
        oya = find(i)
        parent[i] = oya
        #print(oya)
        if oya in group:
            group[oya] = group[oya]+1
        else:
            group[oya]=1
     
    for i in range(K):
        A,B = list(map(int,input().split(' ')))
        if find(A)==find(B):
            bad_list[A]+=1
            bad_list[B]+=1
        
        #union(A,B)
     
     
     
    #print(parent)
    #print('parent')
     
    #print('friend')
    #print(friend_list)
    #print('bad')
    #print(bad_list) 
    #print(group)
     
    kouho = [0]*N
     
    for i in range(1,N+1):
        oya = find(i)
        abc = group[oya]-friend_list[i]-bad_list[i]-1
        kouho[i-1] = abc
     
    for k in kouho:
        print(k,end=" ")
    print()
     
    #print(parent)

Buggy submission (verdict: Runtime Error):
N,M,K = list(map(int,input().split(' ')))

friend_list = [0]*(N+1)
bad_list = [0]*(N+1)
group = {}

parent = [i for i in range(N+1)]

def union(x,y):
    x = find(x)
    y = find(y)
    parent[x] = y

def find(x):
    if parent[x] == x:
        return x
    else:
        parent[x]= find(parent[x])
        return parent[x]



for i in range(M):
    A,B = list(map(int,input().split(' ')))
    friend_list[A]+=1
    friend_list[B]+=1
    
    union(A,B)

for i in range(1,N+1):
    oya = find(i)
    parent[i] = oya
    #print(oya)
    if oya in group:
        group[oya] = group[oya]+1
    else:
        group[oya]=1

for i in range(K):
    A,B = list(map(int,input().split(' ')))
    if find(A)==find(B):
        bad_list[A]+=1
        bad_list[B]+=1
    
    #union(A,B)



#print(parent)
#print('parent')

#print('friend')
#print(friend_list)
#print('bad')
#print(bad_list) 
#print(group)

kouho = [0]*N

for i in range(1,N+1):
    oya = find(i)
    abc = group[oya]-friend_list[i]-bad_list[i]-1
    kouho[i-1] = abc

for k in kouho:
    print(k,end=" ")
print()

#print(parent)